C program to Reverse a Number

In this tutorial, i am going to show you how to reverse a number in c program with the help of for loop, while loop, function, and recursion.

All Programs to Reverse a Number in C

  • C program to Reverse a Number using For Loop
  • C program to Reverse a Number using While Loop
  • C program to Reverse a Number using Function
  • C program to Reverse a Number using Recursion

C program to Reverse a Number using For Loop

#include<stdio.h>
void main(){
    int Num,rev_Num=0,remainder,a; 
    printf("Enter the number to reverse: ");
    scanf("%d",&Num);
    a=Num;
    for(;Num>0;){
        remainder=Num%10;
        rev_Num=rev_Num*10+remainder;
        Num=Num/10;
    }
    printf("Reverse of %d is %d",a,rev_Num);
}

The result of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

C program to Reverse a Number using While Loop

#include <stdio.h>
int main(){
    int Num, rev_Num = 0, remainder;
    printf("Enter the number to reverse: ");
    scanf("%d", &Num);    
    while (Num != 0){
        remainder = Num % 10;
        rev_Num = rev_Num * 10 + remainder;
        Num = Num/10;
    }    
    printf("The reversed number is: %d", rev_Num);
    return 0;
}

The result of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

C program to Reverse a Number using Function

#include <stdio.h>
int rev_Int(int);
int main(){
    int Num, Rev = 0;   
    printf("\nEnter the number to reverse: ");
    scanf("%d", &Num);   
    Rev = rev_Int (Num);
    printf("Reverse of %d is = %d\n", Num, Rev);
    return 0;
}
int rev_Int (int Num){
    int remainder, Rev = 0;    
    while (Num > 0){
        remainder = Num %10;
        Rev = Rev *10+ remainder;
        Num = Num /10;
    }    
    return Rev;
}

The result of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

C program to Reverse a Number using Recursion

#include<stdio.h>
int main(){
    int Num,rev_Num;
    printf("Enter the number to reverse: ");
    scanf("%d",&Num);    
    //Calling our function that will repeat itself
    rev_Num=rev_Func(Num);
    printf("\nThe reversed number is :%d",rev_Num);
    return 0;
}
int sum=0,remainder;
rev_Func(int Num){    
    if(Num){
        remainder=Num%10;
        sum=sum*10+remainder;
        rev_Func(Num/10);
    }
    else
        return sum;
    return sum;
}

The result of the above c program; as follows:

Enter the number to reverse :- 4545
The reversed number is: 5454

Be First to Comment

Leave a Reply

Your email address will not be published.