This C++ Program which displays a given number with its digits reversed. The program uses a while loop to go through each and every digit of the given number, adds it to the reversed number, multiplies it by 10 and adds them up in the loop.
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
*/
#include<iostream>
using namespace std;
int main()
{
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.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check Computer Science Books
- Check C++ Books
- Apply for Computer Science Internship
- Check Programming Books
- Practice Programming MCQs