Answer to Question #334620 in C++ for Jack

Question #334620

Write a C++ program that takes up to 10-digit integer input from user (can be 1 digit, 2 digit, and so on..); passes this to a function which reverses the digits. Finally, the reversed number should be displayed in the main function. For example: when user enters 10-digit like 1234567890 your function will reverse it to 987654321. Do not use strings. Be careful for big integer values. [use functions, decision control]


1
Expert's answer
2022-04-27T18:24:16-0400
#include <iostream>
using namespace std;
int rev(int n){
    int remainder, reversed_number = 0;
    while(n != 0) {
    remainder = n % 10;
    reversed_number = reversed_number * 10 + remainder;
    n /= 10;
  }
  return reversed_number;
}


int main() {


  int n;


  cout << "Enter an integer: ";
  cin >> n;


  cout << "Reversed Number = " << rev(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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS