Showing posts with label reverse of the number. Show all posts
Showing posts with label reverse of the number. Show all posts

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
}
Read More...

C++ Program/Code to input a no. & print its reverse.

3 comments
//In this program code the user will be prompted to enter a number (integer), and the program will //print the reverse of that.

#include<iostream>      //For Input/Output Stream
#include<conio.h>        //For clrscr & getche
using namespace std;   //using the standard namespace
void main()
{
clrscr();                         //clearing the console screen
long int x,y,s=0;            //value of 's' is initialized with a value of '0'
cout<<"Enter no.:";
cin>>x;                          //user input the value of 'x'
while(x!=0)                   //run until the value of 'x' is not equal to '0'
{
y=x%10;                       //getting the remainder from 'x' when divided by '10' & storing it into 'y'
x=x/10;                          //dividing the value of 'x' by '10' & storing the quotient (integer) in 'x'
s=s*10+y;                      //storing the reversed weighted result in 's'
}
cout<<"\nThe reverse no.="<<s; //printing the reversed number
getche();                        //to exit the program after a keystroke
}
Read More...