Reverse a number using loop in C++

In this section you will learn reverse a numbers in c++ using loop. We will use three variable for reverse the numbers. Just simple mathematic operation we will perform to reverse the number. In this program we are taking the input from user.

Reverse mean shifting the first value to last value and shifting the last value to first value.

Let’s suppose a user will input a number ‘386’, then we will perform reverse operation on it.

How program will work?

Program of reverse a number using loop in c++.


	#include<iostream.h>
	#include<conio.h>
		
	//REVERSE OF A NUMBER WITH LOOP
	void main()
	{
		int n, temp, rev = 0;
		clrscr();
		
		cout<<"ENTER A NUMBER TO REVERSE:- ";
		cin>>n;
		temp = n;
	
	while(n != 0)
	{
		rev = rev *10;
		rev = rev + n%10;
		n = n/10;
	}
	cout<<"REVERSE OF IS:- "<<temp<<" "<<rev;

	getch();
	}
	

Output:


	ENTER A NUMBER TO REVERSE:- 386
	REVERSE OF 465 IS:- 683

Post Your Comment