C program to Reverse a Number

C program to Reverse a Number

C program to reverse a number; Through this tutorial, we will learn how to reverse a number in c program using for loop, while loop, function, and recursion.

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 output 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 output 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 output 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 output of the above c program; as follows:

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

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *