Write a function reverseDigit which takes an integer as a pa

Write a function, reverseDigit, which takes an integer as a parameter and returns the number with its digits reversed. For example, the value of reverseDigit (12345) is (54321); the value of reverseDigit (5600) is 65; the value of reverseDigit, (7008) is 8007; and the value of reverseDigit (-532) is -235.

Please use C++ Visual Studio 2013 for this problem

Solution

#include<iostream>
using namespace std;

int reverseDigit(int n)
{
int reverse = 0;
int lastDigit;
  
//Till number becomes zero.
//Get the last digit.
//Each time last digit is present add it to the previous reverse by multiplying reverse*10 and then
//adding last digit.
while(n)
{
lastDigit = n%10;
reverse = reverse*10+lastDigit;
n = n/10;
}
  
//return reverse
return reverse;
}

int main()
{
cout << \"Reverse of 12345 is : \" << reverseDigit(12345) << endl;
cout << \"Reverse of 5600 is : \" << reverseDigit(5600) << endl;
cout << \"Reverse of 7008 is : \" << reverseDigit(7008) << endl;
cout << \"Reverse of -532 is : \" << reverseDigit(-532) << endl;
return 0;
}

OUTPUT:

Reverse of 12345 is : 54321
Reverse of 5600 is : 65
Reverse of 7008 is : 8007
Reverse of -532 is : -235

Write a function, reverseDigit, which takes an integer as a parameter and returns the number with its digits reversed. For example, the value of reverseDigit (1

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site