Algorithm


  1. Declaration of Variables:


  2. originalNumber: The input number to be reversed.
    reversedNumber: The variable to store the reversed number.
    remainder: Temporary variable to store the last digit of originalNumber in each iteration.

    Input:
    The user is prompted to enter an integer, which is stored in originalNumber.

    Reverse the Number:


  3. The while loop runs until originalNumber becomes zero.
    In each iteration:
    remainder is calculated as the last digit of originalNumber (using the modulo % operator).
    reversedNumber is updated by multiplying it by 10 and adding the current remainder.
    The last digit is removed from originalNumber by dividing it by 10.

    Output:
    The reversed number (reversedNumber) is displayed to the user.

 

Code Examples

#1 Code Example-Programing to Reverse a Number Using While Loop

Code - C Programming

#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;

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter the number to reverse: 13547
The Reversed number is : 74531

#2 Code Example-Programing to Reverse a Number Using Recursion

Code - C Programming

#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;

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter the number to reverse: 54653
The Reversed number is : 35645

#3 Code Example-Programing to Reverse a Number Using For Loop

Code - C Programming

#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);

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter the number to reverse: 654
The Reversed number is : 456

#4 Code Example-Program to Reverse a Number Using Function

Code - C Programming

#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;

}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter the number to reverse: 65418
The Reversed number is : 81456
Advertisements

Demonstration


C Programing Example  to Reverse A Number Using Different Methods-DevsEnv

Next
Appending into a File in C Programming