Step 1:
Open the IDE
Step 2:
Type the coding given below:
CODING:
#include<stdio.h>
#include<stdlib.h>
main()
{
int n, i,j,line;
printf("\n\n");
printf("PROGRAM FOR FLOYD'S TRIANGLE\n\n");
printf("enter the number..");
scanf("%d", &n);
line=1;
j=1;
while(line<=n)
{
for(i=1;i<=line;i++)
{
printf("%d", j);
printf("\t\t");
j++;
}
line++;
printf("\n");
}
getch();
}
#include<stdlib.h>
main()
{
int n, i,j,line;
printf("\n\n");
printf("PROGRAM FOR FLOYD'S TRIANGLE\n\n");
printf("enter the number..");
scanf("%d", &n);
line=1;
j=1;
while(line<=n)
{
for(i=1;i<=line;i++)
{
printf("%d", j);
printf("\t\t");
j++;
}
line++;
printf("\n");
}
getch();
}
OUTPUT:
EXPLANATION:
Floyd's triangle is a right-angled-triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:
1 | ||||
2 | 3 | |||
4 | 5 | 6 | ||
7 | 8 | 9 | 10 | |
11 | 12 | 13 | 14 | 15 |
First, the value of line, j, i are assigned as 1.
Here I gave the n value as 5.
Step 1:
Step 1:
1. while(1<=5)true
2. for(i=1;1<=1; i++); True
3. print 1;
4. tab
5. j++ =2.
6. i=2;
7. For(2<=1)False
8. line++ = 2.
2. for(i=1;1<=1; i++); True
3. print 1;
4. tab
5. j++ =2.
6. i=2;
7. For(2<=1)False
8. line++ = 2.
Step 2:
While(2<=5)
for(i=1; 1<=2;i++)
print j. (2).
tab
j++ = (3)
i=2;
for(2<=2)True
print j(3)
tab
j++ = 4
i=3.
for(3<=2)False
Then line++ = 3.
While(2<=5)
for(i=1; 1<=2;i++)
print j. (2).
tab
j++ = (3)
i=2;
for(2<=2)True
print j(3)
tab
j++ = 4
i=3.
for(3<=2)False
Then line++ = 3.
Step 3:
while(3<=5)
for(i=1;1<=3;i++)
print j=4.
tab
j++ = 5.
i=2.
for(2<=3)
print j=5.
tab
j++ = 6.
i=3.
for(3<=3)
print j=6.
tab
j++ = 7
i=4.
for(4<=3)False
Then line++ = 4.
for(i=1;1<=3;i++)
print j=4.
tab
j++ = 5.
i=2.
for(2<=3)
print j=5.
tab
j++ = 6.
i=3.
for(3<=3)
print j=6.
tab
j++ = 7
i=4.
for(4<=3)False
Then line++ = 4.
Same procedure until while condition becomes false.
No comments:
Post a Comment