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

By | July 2, 2019

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

Leave a Reply

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