Practical 5A- Nested Iteration

Posted: January 20, 2012 in Lab 3

Problem Statement-
Write a program that takes the size of each triangle and the number of triangles as input and outputs inverted star triangles as illustrated below:
Example
This program displays a row of inverted triangles.
————————————————–
Enter the size of the triangle: 4
Enter the number of triangles: 3
* * * * * * * * * * * *
* * * * * * * * *
* * * * * *
* * *

#include<stdio.h>
int main()
{
	int a,b,i,j,k;

	printf("This program displays a row of inverted triangles.\n");
	printf("---------------------------------------------------------\n");
	printf("Enter the size of triangle: ");
	scanf("%d",&a);
	printf("Enter the number of triangles: ");
	scanf("%d",&b);
	for(i=0;i<a;i++)
	{
		for(j=0;j<=i;j++)
			printf(" ");
		for(j=0;j<b;j++)
		{
			for(k=0;k<(a-i);k++)
				printf("* ");
			for(k=0;k<(2*i+1);k++)
				printf(" ");
		}
		printf("\n");
	}
	return 0;
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s