1. ## Loop problem?

I have to write a program that takes one integer and follows these rules. First, print this number. When the number is even, change the number to itself divided by 2. Print the resulting number. When the number is odd, change the number to itself multiplied by 3. Then add 1 to the number. Print the result. Continue following these rules until the number you have left is 1 (which will be printed as well).

e.g. given 22 print:
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1

This is what I have so far:
Code:
```#include <stdio.h>

int main()
{
int x;

{
printf("Enter a number: ");
scanf("%d", &x);

if (x % 2 == 0)

printf("%d\n", x / 2 );

else if (x % 2 != 0)

{
printf("%d\n", x * 3 + 1); }
else
{ printf("%d\n", x = 1); }
}

return 0;
}```
When you enter in an even or odd number it works but the program just stops. eg I enter 22 it prints 11 and just stops.

If I do this with a while loop how do I do it?

Any help is appreciated, but please explain.

2. Code:
```while(x != 1)
{
...
}```
Inside your loop, assign the new value to x, don't just print it out.

3. Cprogramming.com Tutorial: Loops
C Assignment Operators
Increment and Decrement Operators (++, --)

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

int main() {
int x;
printf("Enter a number: ");
scanf("%d", &x);
printf("%d\n", x);
/* Keep these outside of the loop */
do {
if (x % 2 == 0)
x /= 2; /* x = (x / 2) */
/* If x is even, assign x to x divided by two */
else {
x *= 3; /* x = (x * 3) */
x++; /* x = (x + 1) */
}
/* If x is not even, assign x to x times three, then add one to it */
printf("%d\n", x);
} while (x != 1);
/* The do-while ensures that x is printed when x equals one */
return 0;
}```
For future reference, use Google to search for specific help and try everything that comes up. Use the forums as a last resort, for when you can't find what you're looking for on Google.

4. hmm....I suggest you use unsigned int or size_t for x.

5. Originally Posted by cph
hmm....I suggest you use unsigned int or size_t for x.
Why would he need to use an unsigned int or a size_t as opposed to a regular int?

6. Originally Posted by Babkockdood
Why would he need to use an unsigned int or a size_t as opposed to a regular int?
just in case...you know...there are naughty users out there

7. Originally Posted by cph
just in case...you know...there are naughty users out there
I don't get it.

8. Originally Posted by Babkockdood
I don't get it.
You've never seen someone intentionally screw up a program so they can get the day off while it's fixed?

I used to do service for a small office where their lan server was crashing almost every other day. Rebooting it, set everything back to rights and it would run error free for a few days. None of us could figure out what was happening until we caught one of the clerks deliberately entering garbage into the entry areas, making it lock up and then calling tech support to get the rest of the day off. Well, as it turned out, she got a lot of days off after the boss caught her at it...

It's not at all uncommon...

9. Originally Posted by CommonTater
You've never seen someone intentionally screw up a program so they can get the day off while it's fixed?

I used to do service for a small office where their lan server was crashing almost every other day. Rebooting it, set everything back to rights and it would run error free for a few days. None of us could figure out what was happening until we caught one of the clerks deliberately entering garbage into the entry areas, making it lock up and then calling tech support to get the rest of the day off. Well, as it turned out, she got a lot of days off after the boss caught her at it...

It's not at all uncommon...
I can't believe I've never thought of that.

10. Originally Posted by CommonTater
You've never seen someone intentionally screw up a program so they can get the day off while it's fixed?

I used to do service for a small office where their lan server was crashing almost every other day. Rebooting it, set everything back to rights and it would run error free for a few days. None of us could figure out what was happening until we caught one of the clerks deliberately entering garbage into the entry areas, making it lock up and then calling tech support to get the rest of the day off. Well, as it turned out, she got a lot of days off after the boss caught her at it...

It's not at all uncommon...
she was smart, but not smart enough. LOL.