Advertisements
Advertisements
Question
Write a program in C++ to reverse a positive integer number input from the keyboard.
Code Writing
Advertisements
Solution
#include <iostream>
using namespace std;
int main() {
long long n, rev = 0;
cout << "Enter a positive integer: ";
cin >> n;
// The loop runs as long as n is greater than 0
while (n > 0) {
int digit = n % 10; // Get the last digit
rev = rev * 10 + digit; // Shift rev left and add the digit
n /= 10; // Remove the last digit from n
}
cout << "Reversed number: " << rev << endl;
return 0;
}
shaalaa.com
Is there an error in this question or solution?
2025-2026 (March) Official Board Paper
