Program of C++ to print number in reversed order using do_while,modulus and divide operator
Code:
#include<iostream>using namespace std;
void main()
{
int num=0; int reversed=0; int rem=0;
cout<<"Enter any number:";
cin>>num;
do
{
rem=num%10;
reversed=reversed*10+rem;
num/=10;
}
while(num!=0);
cout<<"\n"<<reversed;
system("pause");
}
Algorithm of a program to print number in reversed order:
1:Take 3 variables of integer type(number,remainder and reversed) that's is equal to 0.
2:Print input required message on console.
3:Take and store value in 1st variable (number).
4:Use do_while loop with condition that is number is not equal to zero.
5:In loop body we use 3 statements.
6:1st one is take remainder of the entered number by 10 and store it in 2nd variable(remainder).
7:2nd is to multiply the value of reversed variable by 10 and add into the value of remainder.
8:3rd is to store value in variable (number) after divided by 10.
9:Outside the loop body we print the value of variable (reversed).
Comments
Post a Comment