Program to Reverse a Number [C, C++, Python & Java]

Posted in

Program to Reverse a Number [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on October 10, 2024

    This tutorial will help you learn how to write a program to reverse a user-entered number. We will implement a program in C, C++, Python, and Java.

    To write this program, you must know the while loop and the language syntax.

    So, let us get started!

    C Program to Reverse a Number

    #include<stdio.h>
    int main()
    {
        int num, r=0, dig;
        printf("Enter a number: ");
        scanf("%d", &num);
        while(num!=0)
        {
           dig = num%10;
           r = (r*10)+dig ;
           num = num/10;
        }
        printf("The reverse of the number is: %d", r);
        return 0;
    }

    Output

    Enter a number: 45675
    The reverse of the number is: 57654

    C++ Program to Reverse a Number

    #include<iostream>
    using namespace std;
    int main()
    {
        int num, r=0, dig;
        cout<<"Enter a number: ";
        cin>>num;
        while(num!=0)
        {
           dig = num%10;
           r = (r*10)+dig ;
           num = num/10;
        }
        cout<<"The reverse of the number is: "<<r;
        return 0;
    }

    Output

    Enter a number: 2453
    The reverse of the number is 3542

    Python Program to Reverse a Number

    num= int(input("Enter a number: "))
    r=0
    while num!=0:
              dig = num%10
              r =(r*10)+dig;
              num = num//10
    print("The reverse of the number is",r)

    Output

    Enter a number: 44672
    The reverse is 27644

    Java Program to Reverse a Number

    import java.util.*;
    public class Main
    {
        public static void main(String[] args)
        {
        int num, r=0, dig;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        num = sc.nextInt();
        while(num!=0)
        {
           dig = num%10;
           r = (r*10)+dig ;
           num = num/10;
        }
        System.out.println("The reverse of the number is: " +r);
        }
    }

    Output

    Enter a number: 
    8765
    The reverse of the number is: 5678

    For many individuals, it might be confusing to understand the logic inside the while loop. Let us understand it here in detail.

    Let us take a number, 678.

    Iteration 1:

    678 is not equal to 0.

    We will first separate the last digit, 8, using the mod operator.

    dig = num%10 = 678%10 = 8.

    As r is initialized to zero, r = (r*10)+dig = 8.

    num = num/10 = 678/10 = 67.

    Print r, i.e., 8.

    Iteration 2:

    Now, the num is 67. We will separate 7, using the above logic.

    dig = num%10 = 67%10 = 7.

    r = (r*10)+dig = (8*10)+7 = 87.

    num = num/10 = 67/10 = 6.

    Print r, i.e., 87.

    Iteration 3:

    Now, we are left with only 6.

    dig = num%10 = 6%10 = 6.

    r = (r*10)+dig = (87*10)+6 = 876.

    num = num/10 = 6/10 = 0.

    Print r, i.e., 876.

    The while loop stops because the num becomes 0, and you have the reverse of the number 678, i.e., 876.

    Conclusion

    We hope you might have understood the logic for reversing a number. The above example will definitely help you comprehend well. Try to execute the above programs yourself. If you face any difficulties, do let us know in the comments. We would be glad to help you.

    People are also reading:

    Leave a Comment on this Post

    0 Comments