Thread: Reverse of a number

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    2

    Reverse of a number


    Code:
    
    
    #include <stdio.h>
    
    
    main()
    {
        int num,rem,rev=0;
        printf ("Enter the number to be reversed:\n");
    
    
        scanf  ("d",&num);
        while (num>=1)
    
    
        {
            rem = num % 10;
            rev = (rev*10) + rem;
            num = num/10;
        }
        printf("The reverse of number is %d\n",rev);
        ;
    }
    The program compiles ,the output is:

    output:

    Enter the number to be reversed:
    123
    The reverse of number is 26.
    press any key to continue


    The output should be

    Enter the number to be reversed:
    123
    The reverse of number is 321.
    press any key to continue



    ---------------------------------------------------------------------------
    Warnings and errors:

    -------------- Build: Debug in reverse (compiler: GNU GCC Compiler)---------------


    mingw32-gcc.exe -Wall -g -c C:\Users\Ram\Documents\reverse\main.c -o obj\Debug\main.o


    C:\Users\Ram\Documents\reverse\main.c:3:1: warning: return type defaults to 'int' [-Wreturn-type]


    C:\Users\Ram\Documents\reverse\main.c: In function 'main':



    C:\Users\Ram\Documents\reverse\main.c:8:5: warning: too many arguments for format [-Wformat-extra-args]


    C:\Users\Ram\Documents\reverse\main.c:18:1: warning: control reaches end of non-void function [-Wreturn-type]


    mingw32-g++.exe -o bin\Debug\reverse.exe obj\Debug\main.o

    Output file is bin\Debug\reverse.exe with size 28.71 KB


    Process terminated with status 0 (0 minute(s), 0 second(s))


    0 error(s), 3 warning(s) (0 minute(s), 0 second(s))




    -------------- Run: Debug in reverse (compiler: GNU GCC Compiler)---------------


    Checking for existence: C:\Users\Ram\Documents\reverse\bin\Debug\reverse.e xe
    Executing: "C:\Program Files (x86)\CodeBlocks/cb_console_runner.exe" "C:\Users\Ram\Documents\reverse\bin\Debug\reverse. exe" (in C:\Users\Ram\Documents\reverse\.)


  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    >> C:\Users\Ram\Documents\reverse\main.c:8:5: warning: too many arguments for format [-Wformat-extra-args]

    This means something is wrong with your scanf call.

    Code:
    scanf  ("d",&num);
    You are missing something in there. Look carefully.



    "main()" returns an int, which is what the rest of those warnings are trying to tell you:

    Code:
    int main(void)
    {
        // ...
    
        return 0;
    }

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Apart from the small mistake (which you'll soon work out), the formatting, and the definition of main I think it's a nifty solution.

    Edit: By the way, and sorry if you've already done this, you can test the thing works by simply not worrying about getting user input for now... E.g.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int num, rem, rev = 0;
    
        num = 12345;   // Just testing (num is supposed to be supplied by the user)
                       // Should probably try some negative values for num as well
    
        while (num >= 1)
        {
            rem = num % 10;
            rev = (rev * 10) + rem;
            num = num / 10;
        }
        printf("The reverse of number is %d\n", rev);
        
        return 0;
    }
    Gives:
    Code:
    The reverse of number is 54321
    Last edited by Hodor; 01-11-2016 at 11:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse a five digit number in C
    By Aamir Masood in forum C Programming
    Replies: 13
    Last Post: 11-08-2010, 11:37 AM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. Reverse a number
    By a2008 in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2008, 11:04 PM
  4. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  5. recursivly reverse a number
    By doug in forum C Programming
    Replies: 2
    Last Post: 02-24-2002, 09:59 AM

Tags for this Thread