Answer to Question #71813 in C++ for raja haseeb

Question #71813
Q 4. Write a function, reverseDigit that 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.
1
Expert's answer
2017-12-12T13:44:07-0500
#include <iostream>

using namespace std;

//begin task
int reverseDigit(int val)
{
int res = 0;
while (val != 0)
{
//move digits result to left
res *= 10;
//get last digit value
res += val % 10;
//next digit value
val = val / 10;
}
return res;
}
//end task


//TESING
int main()
{
cout << reverseDigit(12345) << "\n";
cout << reverseDigit(5600) << "\n";
cout << reverseDigit(7008) << "\n";
cout << reverseDigit(-532) << "\n";

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
05.09.19, 10:46

Dear Jetender Khatri, You're welcome. We are glad to be helpful. If you liked our service please press like-button beside answer field. Thank you!

Jetender khatri
04.09.19, 22:23

Thanks you for help me.

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS