# Thread: Nested Loop help

1. ## Nested Loop help

hello everyone,

I am trying to achieve a nested loop to post the following on the prompt:

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

I have been told I must use a modulus to achieve this but I am struggling.

I must use the following format, for e.g I completed this code:

Code:

Code:
```  for(i = 9; i >= 1; i-=2)
{
for(j = 1; j <= i; j+=2)
{
printf("%hd " , j);
}
printf("\n");
}```
to achieve this:

1 3 5 7 9
1 3 5 7
1 3 5
1 3
1

so I am unsure where to add the modulus to to the other nested loop, any help would be appreciated. 2. You've got six lines. Then on each line you've got 1, 2, 3, 4, 5, 6 values.

So that bit is easy. Your skeleton is

Code:
```for (i=0; i < 6; i++)
{
for (j = 0; j < 6; j++)
{
x = // fill here, x is based on i and j
}
printf("\n");
}``` 3. How do I achieve the different values on every line?

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

I know how to do it if it was just 1, 1 3 5, 1 3 5 7 9. But how do I work out two different sets of value in the same code. Thanks. 4. Code:
```1
2 4
1 3 5
2 4 6 8
1 3 5 7 9
2 4 6 8 10 12```
If you can not figure out how to do the above try doing

Code:
```1
1 3
1 3 5
1 3 5 7
1 3 5 7 9
1 3 5 7 9 11```
Then look up what !0 equals.

Tim S. 5. I have worked out something similar such as

Code:
```printf("\n\n");

for(i = 2; i <= 10; i+=2)
{
for(j = 2; j <= i; j+=2)
{
printf("%hd " , j);
}
printf("\n");
}```
for

2
2 4
2 4 6
2 4 6 8
2 4 6 8 10

but i am still confused on how to add different lines of numbers in between each other but still in a similar pattern using similar to code that i have used above. 6. Code:
```  for(i = 2; i <= 10; i+=2)
{
printf("Row start = %d\n", i % 2 );
for(j = 2; j <= i; j+=2)
{
printf("%hd " , j);
}
printf("\n");
}```
From this, can you figure out an expression that gives you 1,2,1,2,1,2,.... Popular pages Recent additions #### Tags for this Thread

loop, loops, modulus, nested, nested loops 