Friday, 27 April 2012

Printing the Floyd's Triangle in Normal and Reverse Order in C



One step further: How about this?


1  2  3  4
5  6  7
8  9
10
10
9  8
7  6  5 
4  3  2  1


for n=4;


#include<stdio.h>
#include<conio.h>


int main()
{
n,i,j,a=1,b;

printf("Enter the number of rows in the Triangle: \n\n");
scanf("%d",&n);

for(i=n;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d  ",a);
a++;
}
b=a-1;
printf("\n\n");
}

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d  ",b);
b--;
}
printf("\n\n");
}
getch();
return 0;
}

1 comment: