A simple C++ program in which user enter a number, program reverse it and display reversed number on the console.
For example: If input number is 12345
Then reversed number will be 54321
Concept used: for loop, modulus operator
Post Contains:
Logic Explanation:
Code:
We can also use while(number!=0) instead of for( ; number!=0 ; )
Image view of code:
Program output:
Dry Running The Program
Let input number is 123
so number=123 and reverse=0
number!=0 for loop condition true
reverse = reverse * 10; reverse=0
reverse = reverse + number%10; reverse= 0 + 123%10= 3
number = number/10; number=123/10= 12
number=12 for loop condition(number!=0) true
for loop iteration
reverse = 3 * 10; reverse=30
reverse = reverse + number%10; reverse= 30 + 12%10= 32
number = number/10; number=12/10= 1
number=1 for loop condition(number!=0) true
for loop iteration reverse = reverse * 10; reverse=320
reverse = reverse + number%10; reverse= 320 + 1%10= 321
number = number/10; number=1/10= 0
number=0 for loop condition false loop will break and display the output value of reverse.
For example: If input number is 12345
Then reversed number will be 54321
Concept used: for loop, modulus operator
Post Contains:
- Logic making and explanation
- Reverse number program code
- Image view of code
- Dry run the program step by step
Logic Explanation:
- To make a program like that we need to access every digit individually of entered number from last
- Modulus operator provides such condition so first we take modulus of number until it become zero and add the every single digit which produce by modulus operator into our new integer variable which is named as reverse in the program with at each iteration of for loop
- When for loop is break variable revers will have the reverse of entered number
Code:
#include<iostream>
using namespace std;
int main() {
int number, reverse = 0;
cout<<"Input a Number to Reverse and press Enter: ";
cin>> number; // Taking Input Number in variable number
for( ; number!= 0 ; )
{
reverse = reverse * 10;
reverse = reverse + number%10;
number = number/10;
}
cout<<"New Reversed Number is: "<<reverse;
return 0;
}
We can also use while(number!=0) instead of for( ; number!=0 ; )
Image view of code:
Program output:
Dry Running The Program
Let input number is 123
so number=123 and reverse=0
number!=0 for loop condition true
reverse = reverse * 10; reverse=0
reverse = reverse + number%10; reverse= 0 + 123%10= 3
number = number/10; number=123/10= 12
number=12 for loop condition(number!=0) true
for loop iteration
reverse = 3 * 10; reverse=30
reverse = reverse + number%10; reverse= 30 + 12%10= 32
number = number/10; number=12/10= 1
number=1 for loop condition(number!=0) true
for loop iteration reverse = reverse * 10; reverse=320
reverse = reverse + number%10; reverse= 320 + 1%10= 321
number = number/10; number=1/10= 0
number=0 for loop condition false loop will break and display the output value of reverse.
nyc 1
ReplyDeleteWhy reverse=reverse*10 ??
ReplyDeleteWhy reverse =reverse +number%10 ??
Why number =number/10
Yash Brahmani Kindly Dry Run The Code On a Note Book With A Simple input e.g 123
DeleteHope You Will Get The Logic If you face any Difficulty Ask Here Again.
How to make this program using array
Deleteyou can do this programme using a string.
DeleteI can't get the logic??
ReplyDeleteI know where you lost it.
DeleteDid you think 123%10=12?
It's 3.
I thought like that..:-p
it does not work when you enter 1000 in input
ReplyDeleteHusnain Ali Shah.
DeleteThe Number You Get After Reverse 1000 is 1 It discard the 000 and Give The Actual Number in
1... 1000 -> 0001 = 1
I looked for the logic and explanation everywhere, but din't understand shit. But due to the 'Dry running' section here, I got why are the nos. multiplied and divided that way.
ReplyDeletei didnt understand logic..
ReplyDeleteNice blog, simple logics. Thnx.
ReplyDeletei don't get it, you set reverse = 0, how can it ever be anything different than 0? I'm sorry but i really don't get it.
ReplyDeleteThank You very much . it works!
ReplyDeleteoutput sae show nai ho rai
ReplyDeleteC++ Program to Reverse a Number
ReplyDeleteReverse of number means reverse the position of all digits of any number. For example reverse of 536 is 635
C++ Program to Reverse a Number
ReplyDeleteReverse of number means reverse the position of all digits of any number. For example reverse of 536 is 635
This comment has been removed by the author.
ReplyDeletehow to declare array through a variable?
ReplyDeleteint n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin<<arr[i];
}
for(int x=0;x<n;x++)
{
cout<<arr[x]<<endl;
}
why a value of constant is required in array length, why not a variable(used 'n' as in this case ).
I'll be very helpful if you clarify this doubt of mine.E-mail any solution to
sahu_harshit@yahoo.in
const int n -- needed
Deletewhy the hell is it showing erroor in using namespace std??? it shows decleration syntax
ReplyDeleteprobably your software doesnt contain the namespace compiler
Deleteyour posts are incredible
ReplyDeletethis won't work correctly. this should be in a function and should call itself to reverse the whole number accordingly. also it needs to account for numbers with 0 on the end. if statement(s) would work well enough to account for this
ReplyDeleteOmg thank you so much this is actually smart !
ReplyDeleteBut 6 digit number cannot be reversed
ReplyDelete