Thread: Loop problem?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    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. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    while(x != 1)
    {
      ...
    }
    Inside your loop, assign the new value to x, don't just print it out.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    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. #4
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    hmm....I suggest you use unsigned int or size_t for x.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by cph View Post
    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. #6
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by Babkockdood View Post
    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. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by cph View Post
    just in case...you know...there are naughty users out there
    I don't get it.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    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. #9
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by CommonTater View Post
    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. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by CommonTater View Post
    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.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  3. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  4. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  5. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM