A C++ Program to Reverse the 5 Digit number.

Leave a Comment
/*In this program we find the quotient & remainder. This program runs until the variable 'n' not becomes equal to 0 and during this remainder in multiplied with multiplier variable 'm' which initially equals to 10000(Five Digits ) and as the loops progresses it will reduced further (with division by 10) . The multiplied quotient will be added with reverse number in each iteration and original is also divided 10.*/

#include <iostream.h>             //For general I/O functions
#include <conio.h>                  
//For console related functions
//using namespace std;              
//Uncomment if you are using VISUAL Studio
void main()
{
int n=0,rev=0,r,q,m=10000;    
//initialising n, rev & m in order to avoid any garbage value
cout<<"Enter the Five Digit Number : ";
cin>>n;                                    
//Inputting the number for the user
if(n>99999)                             
//Checking if the number is greater than 5 digit
{
cout<<"Invalid Number"<<endl;
exit(1);                                   
//If true en exit the program
}
while(n!=0)                          
//Repeat the process till the number is not zero
{
r=n%10;                             
//Obtaining the remainder when divided by 10
q=n/10;                               
//Obtaining the quotient when divided by 10
n=q;                                    
//assigning the value of 'q' to 'n'
rev=rev+r*m;                    
//storing the reversed number
m=m/10;                             
//decreasing the 'm' with a factor of 10
}
cout<<"Reverse is : "<<rev;       
//printing the reversed number
getche();                                        
//Wait for the character input to exit the program
}

0 comments: