1. ## nested loops

Hello, I am having problems doing calculations in a loop.
What Im trying to do is- count from number1 to number2- which i did using a FOR loop. So, say the numbers are 3 4 5 6 7, I need to take each one of those numbers and perform a calculation to it based on whether it is odd or even.
I can do the calculation for "number1" and "number2", but am going no where on the numbers in between. Please help!!!

Here's how im counting:
if(n1>n2)
for(i=n2;i<=n1;i++)
else
for(i=n2;i>=n1;i--)

and heres my individual calculations:
while(n1>1)
if(n1%2==0)
n1=n1/2;
else
n1=n1*3+1;

2. If you want to do calculations on a series of numbers after you enter them you will need some kind of array to hold these numbers for later processing.

Jim

3. Looking at your code that you have shown, I can see something that may be a problem

Code:
```//You are not using '{' or '}'
if(n1>n2)
{
for(i=n2;i<=n1;i++)
{
...
}
}
else
{
for(i=n2;i>=n1;i--)
{
...
}
}
```
Is problem 14 from projecteuler? Problem 14 - Project Euler

4. I agree with Click_here. Not using brackets could be the potential issue. Can you post your complete code?

5. heres what I have so far. one loop is starting at the end of the 1st instead of into it. ???
Code:
```#include <stdio.h>

int main(void)
{
int n1; //number1
int n2; //number2
int i;  //cycle length number
i=1;
int c;  //count
c=1;

printf("enter 2 numbers: ");
scanf("%d%d", &n1, &n2);

for(c=n1;c>=n2;c--)
printf("count = %d\n",c );

while(n1>1)

if(n1%2==0)
{n1=n1/2;
printf("number = %d\n", n1);
i++; }
else
{n1=(n1*3)+1;
printf("number = %d\n", n1);
i++; }

printf("cycle length = %d\n", i);

return 0;
}```

6. Originally Posted by brillpsycho
heres what I have so far. one loop is starting at the end of the 1st instead of into it. ???
If you want that the second loop is inside the first loop than you have to move it into the body of the first:
Code:
```for-loop
{
// do stuff
while-loop
{
// do more stuff
}
}```
Click_here and poornaMoksha told you already to use braces.

Some more notes:
Code:
```printf("enter 2 numbers: ");
scanf("%d%d", &n1, &n2);

for(c=n1;c>=n2;c--)```
The program is probably just for yourself but nevertheless here is a bug waiting to happen because you can't be sure that n1 is always bigger than n2.

Code:
``` while(n1>1)
if(n1%2==0)
{n1=n1/2;
printf("number = %d\n", n1);
i++; }
else
{n1=(n1*3)+1;
printf("number = %d\n", n1);
i++; }```
The second and third line in both the if- and the else-body are identical, thus you can move them below:
Code:
```while (n1 > 1)
{
if (n1 % 2 == 0)
n1 /= 2;
else
n1 = n1 * 3 + 1;
printf("number = %d\n", n1);
i++;
}```
Bye, Andreas

7. I added braces, and just for now im assuming the 1st number if the higher of the 2 numbers. With braces it changes it but im still getting an out come of my calculations only being for the 1st number of the sequence, instead of all the numbers between number 1 and number 2

8. I'm guessing that this is Problem 14 - Project Euler which is based on Collatz conjecture - Wikipedia, the free encyclopedia (Read for hints on algorithm) - (I'm up to the 23rd problem on Project Euler and enjoying the challenges. Is anyone else doing them?)

Code:
`'{' and '}'`
the 'for' loop will not include the 'while' loop
- Without the braces a 'for' or 'while' loop will only execute the next line of code up the the ';' (that is the printf under the 'for' loop and the if/else statement under the 'while' loop). Look at the first code block of Andreas post.

Sorry - I think that we posted at the same time
[/edit]

9. Can you post your new code?

10. heres new code:

Code:
```#include <stdio.h>

int main(void)
{
int num1; //1st number
int num2; //2nd number
int i;  //cycle length number
i=1;
int count;  //count from 1st to 2nd number
count=1;

printf("enter 2 numbers: ");
scanf("%d%d", &num1, &num2);

for(count=num1;count>=num2;count--)          //assuming n1 is greater than n2 for now
{      printf("count = %d\n",count );

while(num1>1)
{
if(num1%2==0)                //if number is even
{
num1=num1/2;
printf("number = %d\n", num1);
i++;
}
else                       //if number is odd
{
num1=(num1*3)+1;
printf("number = %d\n", num1);
i++;
}
}}
printf("cycle length = %d\n", i);

return 0;
}```

11. 

I didn't see that the second number was supposed to be higher than the first number - Disregard this post

[/edit]
Code:
`for(count = num1; count <= num2; count--)`

12. At the end of your while statement, 'i' is not being reset,

num is still equal 1 from the last calculation when it goes through your 'for' loop for the second time

-> When your code goes back to the while statement where num is still 1, so it doesn't execute for the next number