Here is source code of the C++ program which displays a given number with its digits reversed. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
* C++ program to Display a given Number with its Digits Reversed
using namespace std;
int val, temp, num = 0;
cout << "Enter the number "
<< "(avoid entering numbers with leading zeroes) : ";
cin >> val;
temp = val;
while (temp != 0)
num = num * 10 + temp % 10;
temp = temp / 10;
cout << "The given number " << val
<< " with its digits reversed is "
<< num << endl;
$ g++ main.cpp $ ./a.out Enter the number (avoid entering numbers with leading zeroes) : 1234 The given number 1234 with its digits reversed is 4321 $ ./a.out Enter the number (avoid entering numbers with leading zeroes) : 00145 The given number 145 with its digits reversed is 541
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
If you liked this C++ Program, kindly share, recommend or like below!