Here's a C program to generate Floyd's triangle using nested for loops with output and proper explanation.
What's a Floyd Triangle? 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
Floyd's Triangle |
# include <stdio.h> # include <conio.h> void main() { int r, i, j, c = 1 ; clrscr() ; printf("Enter the number of rows : ") ; scanf("%d", &r) ; printf("\nFloyd's triangle is : \n\n") ; for(i = 0 ; i < r ; i++) { for(j = 0 ; j <= i ; j++) { printf("%-6d", c) ; c++ ; } printf("\n\n") ; } getch() ; }
Output of above program is
Enter the number of rows : 5
Floyd's triangle is :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Explanation of above program
The program first asks the user to enter number of rows that he/she wants to print. After that the program uses two for loops or nested for loops to draw Floyd's Triangle.
Lets see how these two loops work with an example -
Suppose r = 5. So the outer loop will run for i = 0 to 4 while the inner loop depends on the value of i (outer loop).
Outer loop i = 0, c = 1
- In this case, inner loop executes for j = 0 to i i.e. 0. So inner loop executes only one time for i = 0.
- Inside inner loop, first the value of variable c is printed in a formatted way. Currently c = 1 so 1 is outputted (first line of output). Value of c and j are incremented by 1.
- Inner loop terminates as now j = 1 so j is not less than or equal to 0. Now, outer for loop executes with i = 1.
Outer loop i = 1, c = 2
- In this case, inner loop executes for j = 0 to i i.e. 1. So inner loop executes two times for i = 1.
- Inside inner loop, first the value of variable c is printed in a formatted way. Currently c = 2 so 2 is outputted. Value of c and j are incremented by 1.
- The inner loop executes again with j = 1 and c = 3.
- Again the value of c is printed. And value of c and j are again incremented. (Second line of output is printed at this step)
- Inner loop terminates as now j = 2 so j is not less than or equal to 1. Now, outer for loop executes with i = 2.
Outer loop i = 2, c = 4
- In this case, inner loop executes for j = 0 to i i.e. 2. So inner loop executes three times for i = 2.
- Inside inner loop, first the value of variable c is printed in a formatted way. Currently c = 4 so 4 is outputted. Value of c and j are incremented by 1.
- The inner loop executes two more times printing c = 5 and c = 6 i.e. third line of output. Also j is incremented each time.
- Inner loop terminates as now j = 3 so j is not less than or equal to 2. Now, outer for loop executes with i = 3.
Outer loop i = 3, c = 7
- In this case, inner loop executes for j = 0 to i i.e. 3. So inner loop executes only four time for i = 3.
- Inside inner loop, first the value of variable c is printed in a formatted way. Currently c = 7 so 7 is outputted. Value of c and j are incremented by 1.
- The inner loop executes three more times printing c = 8, c = 9 and c = 10 i.e. fourth line of output. Also j is incremented each time.
- Inner loop terminates as now j = 4 so j is not less than or equal to 3. Now, outer for loop executes with i = 4.
Outer loop i = 4, c = 11
- In this case, inner loop executes for j = 0 to i i.e. 4. So inner loop executes only five time for i = 4.
- Inside inner loop, first the value of variable c is printed in a formatted way. Currently c = 11 so 11 is outputted. Value of c and j are incremented by 1.
- The inner loop executes four more times printing c = 12, c = 13, c = 14 and c = 15 i.e. last line of output. Also j is incremented each time.
- Inner loop terminates as now j = 5 so j is not less than or equal to 3. Now, outer for loop executes with i = 5 making the looping condition false so the outer loop is also terminated.
I wants Floyd Triangle using while loop
That could be done easily by replacing for loop with while loop. Just make the following changes:
i=0;
j=0;
while(i < r)
{
while(j <= i)
{
printf("%-6d", c) ;
c++ ;
j++;
}
printf("\n\n") ;
i++;
}
Can you post the same by just using GOTO statements??
thanks in advance..
I want a program in Floyd triangle I hd output
output is
x
xx
xxx
xxxx
xxxxx
I need program fr above output
Hello There,
Thanks for highlighting this and indicating about Program to Generate Floyd's Triangle in C where more study and thought is necessary.
I started to program with C and have some programming in JAVA. However, I have a small understanding problem with the following simple code below.
I understand that at the command "while (isspace(ch=getc(in)))" the programm reads till if founds a character that is either a " or a blank. This symbol is assigned to the variable delim.
Is it then correct to say that the program starts again at the same position as before and continues to read the string till "((ch=getc(in))!=delim)" is fulfilled?
THANK YOU!! This saved my butt today, I’m immensely grateful.
Gracias,
Vinay