# Reverse Integer
Given an integer and we have to reverse that integer.
Eg num = 1234 on reversing num = 4321
# Source Code - C++
#include <iostream>
using namespace std;
//Function to Reverse Integer
int Reverse (int num)
{
int flag=0, r=0;
//flag to check if number is negative
if(num<0)
flag=1;
//Taking absolute value of number
num=abs(num);
//Reversing number digit by digit
while(num>0)
{
r=r*10+num%10;
num=num/10;
}
//if falg set make number negative
if(flag==1)
r=r*(-1);
return r;
}
//Main Function
int main() {
int num;
cout<<"Enter number to be reversed\n";
cin>>num;
num=Reverse(num);
cout<<"Reversed number is:"<<num;
return 0;
}
Time Complexity : O(n)
Learn More
- Reverse Integer