Reverse a number in C

05 min read

C program to reverse a number is given below. On applying the modulus operator on the given integer, it returns the remainder, which is nothing but the last digit of the given number. We then start building the reversed number by appending the last digit onto the end of the reverse. We multiply reverse by 10 so that the ones column becomes the tens column, the tens column becomes the hundreds column, and so on.

 

#include <stdio.h>
int main()
{
int n, revnum= 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
 {
 remainder = n%10;
 revnum = revnum*10 + remainder;
 n /= 10;
 }
printf("Reversed Number = %d", revnum);
return 0;
}

 

Output

Enter an integer: 5634 Reversed Number = 4365
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error