Friday, June 18, 2021
Home coding Write a program to reverse given number.

Write a program to reverse given number.

Given a number we have to calculate the reverse of a number .

So what is reverse of number?

reverse of number means reverse the position of all digits of number.

Example:

input:21
output:12

input:32
output:23

Logic:

just find the reminder of number and form new number from rem.

#include<iostream>
using namespace std;
int main()
{
 int num;
 cout<<"Enter a number : "<<endl;
 cin>>num;
 int rev=0;
 while(num>0)
 {
     int rem=num%10;
   rev=rev*10+rem;
     num=num/10;
 }
cout<<"reverse of number is "<<rev<<endl;
 

}

Output:

Enter a number : 14
reverse of number is 41

Time Complexity:O(N)

Space complexity-O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments