Answer to Question #80239 in C++ for Samuel Midzi

Question #80239
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
2018-08-29T04:51:18-0400
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
long reverseDigit();
cout << reverseDigit() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
long reverseDigit()
{
cout << "Enter number = \n";
long num, num1;
cin >> num;
char s[30], s1[30];
ltoa(num, s, 10);
ltoa(num, s1, 10);
int is = strlen(s);
if (s[0] == '\-')
{
for(int i = 1; i < is ; i++)
s[i] = s1[is - i];
}
if (s[0] != '\-')
{
for(int i = 0; i < is; i++)
s[i] = s1[is - i - 1];
}
num = atol(s);
return num;
}

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS