Answer to Question #313989 in C++ for jane

Question #313989

Write a program which takes a 9-digit number input from user, converts it into its reverse

and then display one of the following statements: (10 marks)

• Original number is x steps bigger (where x is the difference between the two)

• Reversed number is x steps bigger

• Both numbers are equal; hence it is a palindrome.


1
Expert's answer
2022-03-19T02:24:26-0400
#include <iostream>
using namespace std;


long reverse(long x, int n)
{
    int d;
    long res = 0;
    for (int i=0; i<n; i++) {
        d = x % 10;
        x /= 10;
        res *= 10;
        res += d;
    }
    return res;
}


int main() {
    long num, rev;

    cin >> num;
    rev = reverse(num, 9);
    if (num > rev) {
        cout << "Original number is " << num - rev
             << " steps bigger" << endl;
    }
    else if (rev > num) {
        cout << "Reversed number is " << rev - num 
             << " steps bigger" << endl;
    }
    else {
        cout << "Both numbers are equal; hence it is a palindrome." << endl;
    }

    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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog