C++ | Reverse of number

Introduction

This is a program to create a reverse of the number. The first one creates reverse number using while loop. The last one also creates the reverse number using while loop.The first one and last one are same but just the first one saves reverse number in variable but last one directly prints it. There is no way to expand the program. Go enjoy the program. Lets begin………….

Program to create a reverse of a number

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int no,rev=0,m;
//Input the number
cout<<"Enter the no."<<endl;
cin>>no;
//using while loop create a reverse number.
while(no>0)
{
m=no%10;
rev=(rev*10)+m;
no=no/10;
}
//show the output reverse number
cout<<"Reverse is "<<rev;
//get character
getch();
}

Output

Enter the no.
1234
Reverse is 4321

Program to create a reverse of the number

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int no,rev=0,m;
//Input the number
cout<<"Enter the no."<<endl;
cin>>no;
//uing while loop create a reverse number.
while(no>0)
{
m=no%10;
cout<<m;
no=no/10;
}
//get character
getch();
}

Output

Enter the no.
5678
8765

How does it work

  1. You enter the number.
  2. The reverse is created using loop and is save or printed.
  3. The reverse is printed( in first program).

Extending it

The program cannot be extended.

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int (name them as you want.)
  5. Add the cout and cin of number.
  6. Add the loop.
  7. Print the reverse.

At the end

You learnt creating the c++ program of creating reverse of number. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

One thought on “C++ | Reverse of number

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s