Thread: Reversing digits in C

  1. #16
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by ssharish2005 View Post
    Code:
    #include <stdio.h>
    
    int main()
    {
        int num = 1234;
        int i = 10;
           
        while(num)
        {
           printf("%d", num % i);
           num /= i;
           i *= 10;
        }
           
       getchar();
       return 0;
    }
    ssharish
    thanks for replying, but the problem is already solved .. and just so you know, we were not allwed to use "while"

  2. #17
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by Adak View Post
    Say you had a number (integer) from the user: 180, 759.

    Now we begin to print it backwards, where un = user number:

    Code:
    /*this is a code idea - won't run as is */
    temp_un = un
    if temp_un > 0 && temp_un % 10 != 0 {
       print temp_un % 10  - the left over 1's place value
       temp_un = temp_un / 10 - integer division, drops any value less than 1 to 0, perfect for this.
    }
    The normal way to do this would be to use a loop (perhaps a while loop, until temp_un was == 0).

    If you haven't had loops yet, you could just repeat this if statement, as many times as you needed to in order to cover the whole range of possible positive integers.


    thanks for posting, problem already solved :P cheers. but i got your idea, its a good one

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It worked.. but they did not tell us about return 0 as well as of yet so i modified it like the following
    As MacGyver mentioned, do not use void main(), use int main(). On a newer version of the C Standard, you should be able to get away with int main() without return 0, but I would rather leave the return 0 there anyway, at least for C.

    clrscr() and getch() are both non-standard and your program does not need them, as far as I can tell.

    they also didnt teach us anything about libraries like include etc so thats why
    Is this in school? If not, I suggest that you quit and demand your money back. They are hopeless in teaching C, and probably in writing C as well.

    because 1234 shoud be inteegers
    No 1234 is an integer, not four integers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Quote Originally Posted by laserlight View Post
    As MacGyver mentioned, do not use void main(), use int main(). On a newer version of the C Standard, you should be able to get away with int main() without return 0, but I would rather leave the return 0 there anyway, at least for C.

    clrscr() and getch() are both non-standard and your program does not need them, as far as I can tell.


    Is this in school? If not, I suggest that you quit and demand your money back. They are hopeless in teaching C, and probably in writing C as well.


    No 1234 is an integer, not four integers.

    well our teacher said to use void main as it will eliminate many errors etc but ok i'll be using int main as well thanks... and return 0 maybe they will tell us about it later, as they have not as of yet..


    yes this is in college.. not in some other institution hehe :P

    clrscr will clear the screen if we dont clear the screen then the old date will also flash on the screen while u running the program in the compiler and without getch the program will execute and quit directly and it wont stay on screen..

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    well our teacher said to use void main as it will eliminate many errors etc but ok i'll be using int main as well thanks... and return 0 maybe they will tell us about it later, as they have not as of yet..
    You might want to read the FAQ on main().

    clrscr will clear the screen if we dont clear the screen then the old date will also flash on the screen while u running the program in the compiler and without getch the program will execute and quit directly and it wont stay on screen..
    If you do want to use them, you should include the relevant header. Chances are it is <conio.h>, but this being non-standard, there is no guarantee that this is the case, and these functions may not even be available. You can roughly simulate getch() using the standard function getchar() from <stdio.h>, but there is no portable and standard way of clearing the screen.

    Out of curiosity, what compiler (and IDE) are you using?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > well our teacher said to use void main as it will eliminate many errors

    Really?! Like what?

    Just curious. If he hasn't said anything about it then he's lying worse than most people.

  7. #22
    Registered User
    Join Date
    Oct 2010
    Posts
    1
    Code:
    #include <stdio.h>
    
    int main()
    {
        long int num,i,x;
        i=10;
        printf("Enter the integer\n");
        scanf("%d",&num);
        printf("\nEntered number is %d\n",num);
       while(num)
        {
           x = num;
           printf("%d", num % i);
           num /= i;
    
        }
    }

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read the forum rules before digging up more dead threads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 04:05 PM
  3. Reversing Digits Project (Help Please)
    By fenixataris182 in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2005, 01:08 PM
  4. Reversing the order of the digits?
    By Mak in forum C Programming
    Replies: 15
    Last Post: 10-09-2003, 09:17 PM
  5. reversing digits
    By lizardking3 in forum C++ Programming
    Replies: 15
    Last Post: 03-28-2003, 12:30 PM