# 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.

Expert's answer

#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;

}

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 orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment