C Program to Reverse a Number

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

How does the inverse of a number get calculated?

The very basic things to do to reverse a number are:

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

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

Reverse a number using a for loop in C

This program was created using a "for" loop. The question is: write a program in C that reverses a number using a 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 built and runs in the Code::Blocks IDE. Here is its sample run:

c program to reverse number

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

reverse a number in c

The dry run of the above program goes like this:

Reverse a number using a while loop in C

Now let's create the same program using a 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, there is no initialization or updating in the while loop.It contains only the condition checking statement. As a result, before running the while loop, we've already assigned a value, say, 0 to the rev variable.That is, the initialization part (related to or having to be used in the while loop) must be done before the loop, and the update part must be used inside the loop's body. The rest of the code is the same as in the previous program using the for loop.

Reverse a Number Without Using a Loop in C

Now, let's make the same program without using any kind 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 produces the same output as the previous one.

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

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

Reverse a number using a user-defined function

The question is: write a program in C to reverse a given number using the 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 the very first program given in this article.

Reverse a Number Using an Array

This is the last program for reversing a number. It uses an 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;
}

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!