What is meant by Reversing the number?
If we took a number 4567 then the reverse of the number should be 7654.
Algorithm:
Step 1: Start.
Step 2: Read n and initialize variable rev to 0.
Step 3: While n is not equal to 0 go to step 4 otherwise go to step 7.
Step 2: Find rem=n%10.
Step 2: Calculate rev=(rev*10)+ rem.
Step 2: Calculate n=n/10 and go to step 3.
Step 2: Print rev.
Step 2: Stop.
Program:
#include <iostream>
using namespace std;
int main()
{
int n,rem,rev=0; //initializing rev to 0
cout<<"Enter a number: ";
cin>>n;
while(n!=0) /* loop for finding the reverse of a number */
{
rem=n%10;
rev=(rev*10)+rem;
n/=10; // n/=10 means n=n/10
}
cout<<"Reverse of the number is: "<<rev;
return 0;
}
Output:
Enter a number: 4567
Reverse of the number is: 7654
C++ Program to print reverse of a given number
Reviewed by Beast Coder
on
January 24, 2021
Rating:
No comments: