Write a program in C++ & Python to reverse a number

Posted in

Vinay Khatri
Last updated on June 10, 2022

    Here in this program, we will reverse a number. First, we will ask the user to Enter a number and then use an algorithm to reverse a number and print it.

    C++

    #include<iostream.h>
    #include< conio.h>
    #include<stdio.h>
    void main()
    {
    clrscr();
    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 is:  "<<r;
    getch();
    }
    Output:
    Enter a number: 2453
    The reverse is 3542

    Python:

    num= int(input("Enter a number: "))
    r=0
    while num!=0:
                    dig = num%10
                    r =(r*10)+dig;
                    num = num//10
    print("The reverse is",r)
    Output:
    Enter a number: 44672
    The reverse is 27644
    People are also reading:

    Leave a Comment on this Post

    0 Comments