# Need Help With Simple Iteration Program

• 03-09-2011
rbreakey
Need Help With Simple Iteration Program
I have been asked to write a program that creates the following pattern without simply printing this sequentially :

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

So far this is my code but it doesn't work. I am meant to use either a for loop or a while loop.

Code:

```#include <stdio.h> int main() {         int i;         int max;         scanf("%d%c", &max);         while(max <= 10)         {                 for(i = 1; i < max; i++)                 {                         printf("%d\n", i);                         max--;                         printf("%d\n", i);                 }         }                 return(0); }```
Can anyone help me?
• 03-09-2011
Salem
Why are you comparing max to 10, when max is decreasing?

You probably don't want to decrease max INSIDE your inner loop.
• 03-09-2011
rbreakey
Code:

```#include <stdio.h> int main() {         int i;         int max;         scanf("%d%*c", &max);                 for(i = 1; i < max; i++)                 {                         printf("%d", i);                 }                 return(0); }```
I have refined it to this which gives me the first line of the question.

Can anybody tell me how to repeat it so that i obtain the pattern in the question?
• 03-09-2011
Salem
You modify the loop you had, not remove it altogether!
• 03-09-2011
rbreakey
Code:

```#include <stdio.h> int main() {         int i;         int max;         scanf("%d%*c", &max);         while(max <= 10 && max >=2)         {                 for(i = 1; i < max; i++)                 {                         printf("%d", i);                 }                                 printf("%d\n");                 max--;         }                                return(0); }```
This gives me the right answer except it produces a 0 at the end of each line.

Code:

```#include <stdio.h> int main() {         int i;         int max = 10;         while(max <= 10 && max >=2)         {                 for(i = 1; i < max; i++)                 {                         printf("%d", i);                 }                 printf("\n");                 max--;         }                        return(0); }```
This is my final code which produces the right result. Can anyone tell me if it looks ok? and if there is anything i need to modify. Cheers.
• 03-09-2011
Salem
It seems OK to me.

One small nit is that the () around return 0 are redundant.
• 03-09-2011
Mr.777
Whole code is working fine.....
And Salem, does this () really matters in output???
I don't think so...... And program shouldn't give 0 in the end....
I made a dry run of this program and it's working nice....