Thread: Print backwards? Can someone please help me?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Print backwards? Can someone please help me?

    Hello,
    I am asking the user for an integer and then I need to print out the number that the user entered backwards. I am having a hard time doing this! I know how this achieved, you just take the "x%10"(mod) of the number and print after each time until the number reaches zero.

    for example: user enters: 1557
    1557%10= 7 ->print
    155%10= 5 -> print
    15%10= 5 ->print
    1%10=1 ->print for the last time b/c after getting 1 then next mod is zero.

    My program is incorrect and I know what I am doing wrong, but I dont know how to create the right code, can someone please help?


    Code:
    #include <stdio.h>
    
    
    int main() {
    
     int num, i;
     printf("Enter a positive integer.\n");
     scanf("%d", &num);
    
     while (num > 0) {
        
    	num=(num%10);
    	
    printf("%d", num);
    num--;	
    
    
    }
    
    
    
    
    system("PAUSE");
    return 0;
    
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Replace num-- by num /= 10;

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You also need to get rid of this line:

    Code:
    num=(num%10);
    Since you are reducing num to the last digit, it will be the only digit that will be printed before it reaches zero.

    Do the modulus directly in the print or use a temporary variable. What you are doing in the above line is assigning the result of num%10 to num, so if you enter 1234, after the first iteration num will be 4. Then as claudiu pointed out, you will need to make your number ten times smaller to get to the next digit.

    Code:
    printf("%d", num%10);
    Last edited by Subsonics; 04-22-2010 at 08:52 PM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by claudiu View Post
    Replace num-- by num /= 10;
    What you said is useless, I appreciate the effort though.
    num /= 10 does not do anything it does not find the remainder. Maybe you meant num%=10 but that is the same as what I have in my code--> num= num %10.

    Any other ideas? The comment directly above me isn't working either.

    Thanks though, trying stuff out and adjusting my code helps my understanding even if it does not work correctly at first.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You will need to divide num by ten. With the adjustment you code would look something like this:

    Code:
    #include <stdio.h>
    
    int main()
    {
            int num;
            printf("Enter a positive integer.\n");
            scanf("%d", &num);
    
            while (num > 0) {
                    printf("%d", num%10);
                    num /= 10;
            }
    
            return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matthayzon89 View Post
    What you said is useless, I appreciate the effort though.
    num /= 10 does not do anything it does not find the remainder. Maybe you meant num%=10 but that is the same as what I have in my code--> num= num %10.

    Any other ideas? The comment directly above me isn't working either.

    Thanks though, trying stuff out and adjusting my code helps my understanding even if it does not work correctly at first.
    How the !@%@#$% do you think you're going to get from 1557 to 155 except by dividing by 10? HINT: % is not division. DOUBLE HINT: You don't want num = num % 10.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Compose a string from a number, reverse the string, then make the string a number again. sscanf and sprintf will be helpful functions. You have to avoid moving the '-' character but that's all there is to it, OP.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    How the !@%@#$% do you think you're going to get from 1557 to 155 except by dividing by 10?
    By subtracting 1402?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by matthayzon89 View Post
    What you said is useless, I appreciate the effort though.
    num /= 10 does not do anything it does not find the remainder. Maybe you meant num%=10 but that is the same as what I have in my code--> num= num %10.

    Any other ideas? The comment directly above me isn't working either.

    Thanks though, trying stuff out and adjusting my code helps my understanding even if it does not work correctly at first.
    Lol, I'm sorry i'm being useless right now, but you do really need to divide by 10 if you hope to print every digit of your number. Right now your while is probably going to iterate forever.

    Get this: say num = 123.

    Code:
    while(num > 0){
      num = num % 10; /* now num = 123 %10 = 3; */
    }
    Second run:

    Code:
    while(3 > 0){
      num = 3 % 10; /* num is still 3 */
    }

    And so on forever.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    LOLOLOL!!!

    Haha

    Thanks for the advice guys! You right. While working on this problem I noticed that THAT dividing by 10 is the only way but I was trying to either divide by 10 OR use mod%10, I didn't think of combining them which was my mistake.

    Thanks for the help

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And here I was gong to suggest a simple hand held mirror! Always too late!!

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by Adak View Post
    And here I was gong to suggest a simple hand held mirror! Always too late!!
    haha that's technically getting the job done.
    I'm sure I would receive partial credit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Towr of Hanoi move the disc
    By WatchTower in forum C Programming
    Replies: 9
    Last Post: 07-17-2009, 03:48 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. how to print a string backwards
    By Unregistered in forum C# Programming
    Replies: 2
    Last Post: 01-27-2002, 01:04 PM