C++ Program to Reverse a Number
Here you will get an example code of C++ Program to Reverse a Number.
You can use the modulo operator (%) and a loop to reverse a number. The modulo operator(%) is used to get the remainder by division operation. Using the modulo operator to divide the number by 10, we can extract the last digit. Then multiply reversed number by 10, and add the extracted digit. This process can be repeated until there are no more digits to extract.
Example of C++ program to reverse a number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { int num,num1,rem, rev=1; cout<<"Enter a Numbers: "; cin>>num; num1=num; while(num!=0) { rem=num%10; num=num/10; rev=(rev*10)+rem; } cout<<"\nReverse of "<<num1<<" is "<<rev; return 0; } |
Output
Check out our other C++ programming Examples