codescracker
c

C Program to Reverse a Number



« Previous Program Next Program »

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

How Reverse of a Number gets Calculated ?

The very basic things to reverse a number are:

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

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:

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:

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 »



© Copyright 2021. All Rights Reserved.

CodesCracker