Idea please

1
2
3
4
5
6
7
8
9
10
/* Shown below is an Floyd's triangle*/
1
2 3
4 5 6
7 8 9 10
11 .... 15
..
..
79 ......... 91
/* Give me the idea to generate this, thankq */
the first loop goes from i=1 to i <= 91
the second inner loop goes from j=1 to j <= i
print out i in the inner loop
print out end of line after the inner loop
I tried like this in C.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()
{
	int i, j;
	
	for (i = 1; i <= 91; i++)
	{
		for (j = 1; j <= i; j++)
			printf("%d ", i);
	
	printf("\n");
	}
	
	return 0;	
				
} 


but the code gives the wrong output.

1
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5
.............
.............
91 91 ..............91


I know the variable i and j used here are to define the row and column but we don't exactly know the no of row.
Last edited on
Ok after a little bit hard work. I solved this but with little bit odd way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int main()
{
	int i, j, num = 1;
	
	for (i = 1; i <= 91; i++)
	{
		for (j = 1; j <= i; j++)
		{
			printf("%d ", num);
			num++;
			if (num == 92)
				break;
		}
		
		printf("\n");
		if (num == 92)
			break;
	}
	
	return 0;
		
}



1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
...
...
79 80 81 82 83 84 85 86 87 88 89 90 91

Ok thanks coder777, Actutually i have to this using goto statment, I will do it.
Last edited on
I solved this but with little bit odd way.
Ok, I didn't elaborate fully.
I'd say that your solution is completely ok. I don't see why it's supposed to be odd.

if you don't want the breaks you'd need to know the number of rows (on line 7) and do some more calculations
Topic archived. No new replies allowed.