Description of reverse the number problem


The main program is to reverse the number. If a user gives the input 12345 then we will make it reverse like 54321 and print it. We have to avoid zero at the beginning of our output.

We have to take the testcase as input from user and for each test case the program will take an input integer and print the reverse of that number. This problem is taken from CodeChef for practice and learning purpose only. We will try to describe the solution and the cause behind it when we solve this problem in this guide.

You can solve this problem in many way using different logic. You should think about your logic. We will give a simple solution to this problem bellow. Let’s try to solve reverse the number problem by C.

Solution of reverse the number problem in C

We have taken an integer Test for storing the given number of test case for this reverse the number program.

For each test case we will take the number which we have to reverse. The we will use a loop until the number is equal to zero. We have used while loop here. Inside the while loop we will determine reminder by dividing the given number by 10. This reminder will be the last digit of the number as like as bellow.

When we divide 25 by 10, the reminder will be 5 which is the last digit of this number 25. That means we have got the first digit of our output.

As we have take the variable revNum = 0.

So, (revNum * 10) + rem = 5

The we will update the number by divide it with 10 which will always give an integer.

After completing the function of while loop we will get the reverse number. Then we will print that number. Now, let’s see the code.

// solution of reverse the number problem by C

#include <stdio.h>

int main(){
  int Test, i;
  scanf("%d", &Test);

  for(i = 1; i <= Test; i++){
    int  n, rem, revNum = 0;
    scanf("%d", &n);

    while(n != 0){
        rem = n % 10;
        revNum = (revNum * 10) + rem;
        n = n / 10;

    printf("%d\n", revNum);

  return 0;

Output of reverse the number program

After compiling and running this reverse the number program you will see the output like this.


Here, the first line is the test case given by user. Then the second line is the first input which we have to reverse using out program. The third line is the output for the first input.

Then the fifth line is the output for the input of fourth line and so on. You many have noticed that for the last test case we have printed 32 instead of 0032. You will get error if this solution prints 0032.

See our other reverse number programs to get clear idea about different way to reverse a number using C programming language.

Previous pageChef and operators

Next page:  Second largest number