C Program to Reverse a Number

In this article, you will learn and get code on reversing a given number by user at run-time in following ways:

  • Reverse a Number using for Loop
  • Reverse a Number using while Loop
  • Reverse a Number without using Loop
  • Reverse a Number using user-defined Function
  • Reverse a Number using an Array

How Reverse of a Number gets Calculated ?

The very basic things to reverse a number are:

  • Find the remainder of that number one by one until the number becomes equal to 0
  • While finding the remainder, initialize the new value to a variable that holds reverse of the number
  • Every time while initializing the new value to rev variable (that holds reverse of given number), don't forgot to multiply 10 with its previous value and add the remainder

For example, let's suppose if user has enters a number say 1234 (initialized to num variable). Then here are the steps:

  • Initially rev=0 (it holds the reverse of given number)
  • Find the remainder of given number using rem=num%10 or rem=1234%10 or rem=4
  • Initialize the new value to rev variable
  • That is, rev=(rev*10)+rem or rev=(0*10)+rem or rev=0+4 or rev=4
  • Now divide the number with 10
  • That is, num=num/10 or num=1234/10 or num=123
  • Check the number, that whether it is equal to zero or not
  • If it is not equal to zero, then continue the operation with new value of rev and num
  • That is, rem=num%10 or rem=123%10 or rem=3
  • And rev=(rev*10)+rem or rem=(4*10)+3 or rem=43
  • Now num=num/10 or num=123/10 or num=12
  • Because num is again not equal to 0, then continue the operation again
  • In this way, we will have a rev variable that holds the reverse of given number (1234)
  • That is, rev=4321

Now let's move on to the programs on reversing a number.

Reverse a Number using for Loop

This program is created using for loop. The question is, write a program in C that reverse a number using for loop. Here is the answer to this question:

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, rev, rem;
    printf("Enter the Number: ");
    scanf("%d", &num);
    for(rev=0; num!=0; num=num/10)
    {
        rem = num%10;
        rev = (rev*10)+rem;
    }
    printf("\nReverse = %d", rev);
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

c program to reverse number

Now enter any number say 1234 and press ENTER key to the following output:

reverse a number in c

The dry run of above program goes like:

  • When user enters a number say 1234, then it gets initialized to num
  • That is num=1234
  • Now in for loop, 0 gets initialized to rev. That is rev=0. And evaluates the condition of the loop, that is, checks whether the value of num is not equal to 0 or not
  • The condition evaluates to be true, because the value of num is not equal to 0
  • Program flow goes inside the loop and num%10 or 1234%10 or 4 gets initialized to rem. And rev*10+rem or 4 gets initialized to rev
  • Now rev=4
  • After executing all the statement, program flow goes to the update part of the loop (that is, its third statement) and num gets divided by 10
  • Now num=123
  • And then evaluates the condition the loop again, that is, checks whether the value of num is not equal to 0 or not
  • The condition again evaluates to be true, program flow goes inside the loop and process the same steps with new value of num and rev
  • Continue the operation, until the value of num becomes equal to 0
  • After exiting from the loop, we will have a variable named rev that holds the reverse of given number
  • Print the value of rev on output

Reverse a Number using while Loop

Now let's create the same program using while loop.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, rev=0, rem;
    printf("Enter the Number: ");
    scanf("%d", &num);
    while(num!=0)
    {
        rem = num%10;
        rev = (rev*10)+rem;
        num = num/10;
    }
    printf("\nReverse = %d", rev);
    getch();
    return 0;
}

Unlike the for loop, in while loop, there is no any initialization and updatation part. It contains only the condition checking statement. Therefore we've already initialized the value say 0 to rev variable before executing the while loop. That is, the initialization part (related to or have to used in while loop) must be done before the loop, and the update part must be used inside the loop's body. Rest of the things are same as given in above program using for loop

Reverse a Number without using Loop

Now let's create the same purpose program, but without using any type of loop.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, rev=0, rem;
    printf("Enter the Number: ");
    scanf("%d", &num);
    CODESCRACKER:rem = num%10;
    rev = (rev*10)+rem;
    num = num/10;
    if(num!=0)
        goto CODESCRACKER;
    printf("\nReverse = %d", rev);
    getch();
    return 0;
}

This program produce the same output as of previous one.

Here we've used the goto keyword. To use this keyword, we must have to create a LABEL. That is, if the name of LABEL is CODESCRACKER, then we can use goto keyword to send the program control flow to start from that place where the LABEL named CODESCRACKER is given. Therefore, the LABEL named CODESCRACKER is created before the goto statement. it works like:

  • When the condition of if statement evaluates to be true
  • Then the statement
    goto CODESCRACKER;
    gets executed. That tells the compiler to go to (or start again from) label named CODESCRACKER
  • Therefore, program flow starts after the LABEL CODESCRACKER
  • That is, again the statement
    rem = num%10;
    gets executed and after this, another two statement (before if statement) gets executed
  • After executing all the three statements present in between the if block and label CODESCRACKER, checks the condition of if block again, if it again evaluates to be true, then program flow again goes to the label with name provided after the keyword goto
  • Program flow continue going to the label until the condition of if block evaluates to be false

Note - The goto statement is used to send the program control flow at any place in the program.

Reverse a Number using user-defined Function

The question is, write a program in c to reverse a given number using user-defined function, findRev(). Here is its answer:

#include<stdio.h>
#include<conio.h>
int findRev(int);
int main()
{
    int num, rev;
    printf("Enter the Number: ");
    scanf("%d", &num);
    rev = findRev(num);
    printf("\nReverse = %d", rev);
    getch();
    return 0;
}
int findRev(int num)
{
    int rev=0, rem;
    while(num!=0)
    {
        rem = num%10;
        rev = (rev*10)+rem;
        num = num/10;
    }
    return rev;
}

It will also produce the same output as of very first program given in this article.

Reverse a Number using Array

This is the last program of reversing a number. It uses array to do the same task that previous programs have done.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, rem, arr[10], i=0, count=0;
    printf("Enter the Number: ");
    scanf("%d", &num);
    while(num!=0)
    {
        rem = num%10;
        arr[i] = rem;
        num = num/10;
        i++;
        count++;
    }
    printf("\nReverse = ");
    for(i=0; i<count; i++)
        printf("%d", arr[i]);
    getch();
    return 0;
}

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋