Thread: Reverse a five digit number in C

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Unhappy Reverse a five digit number in C

    Create program with following transformation:
    1- Reverse a five digit number
    you can only use printf, scanf, arithmetic operators, comparison
    operators and selection statements i.e. if and if‐else.
    Last edited by Aamir Masood; 11-05-2010 at 04:26 PM.

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You can treat the number as a string. Set proper memory constraints on the string variable. Set up a looping structure to print the string array in reverse order.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Aamir!

    Post up your code, and tell us what has you stumped with this problem.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    u can only use integers and floats with basic mathematical operands , comparison operations and selection statements if and if-else without using loops and strings

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You can do this with the modulus operator then.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, it's a little awkward, but right off I'd think some int variables like one, two, three, etc., could be used to hold the digits as you peeled them off the original number:

    int one, two, three, num=345;
    one=num% 10;
    num /10;
    two=num%10
    num /10
    kind of logic.

    What method has your instructor suggested for this assignment? What have you tried?

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Post up what code you have thus far.

    This isn't a homework forum, man...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Actually I had used this code and it also worked but everyone is using this code. I wanted some other way to do it .please.

    Code:
    #include<stdio.h>
    int main()
    {
    int a,b,c,d,e,f,g,i,h,j,sum;
    float o,p,q,r;
    
    printf("Enter the five digit number\n");
    scanf("%d",&a);
    
    b=a%10;
    c=a/10;
    d=c%10;
    e=c/10;
    f=e%10;
    g=e/10;
    i=g%10;
    j=g/10;
    
    printf("%d%d%d%d%d\n",b,d,f,i,j);
    
    sum=b+d+f+i+j;
    printf("Sum of digits = %d",sum);
    
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Without using loops, your options are very limited for doing this. I believe my idea of naming and using variables (posted above), was "cleaner" than your last posted code, but it still amounts to the same basic logic.


    There are other ways to do this, but none of them are simple enough.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Quote Originally Posted by Adak View Post
    Without using loops, your options are very limited for doing this. I believe my idea of naming and using variables (posted above), was "cleaner" than your last posted code, but it still amounts to the same basic logic.


    There are other ways to do this, but none of them are simple enough.
    like???

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Working with the individual bits in the number, and using the base 2 number line.

    Using recursion

    Oh! What about using sscanf() ? That's the string scanning function sister of scanf(). That would be easy.

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    5-digit number you say? You can do this: 99999 - number
    Devoted my life to programming...

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
     99999
    -12345
    ======
     87654
    ?

  14. #14
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I was just joking! I think i saw it somewhere.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number user input
    By jimtuv in forum C Programming
    Replies: 62
    Last Post: 06-13-2010, 11:08 AM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Display Reverse Number, no strings or arrays
    By John Smith in forum C Programming
    Replies: 7
    Last Post: 12-08-2004, 05:29 AM
  4. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  5. Replies: 4
    Last Post: 07-14-2003, 08:11 AM