-->





Translate

Reverse a Number in C++ Program Code

26 comments
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:
  1. Logic making and explanation
  2. Reverse number program code 
  3. Image view of code
  4. 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.

26 comments:

  1. Why reverse=reverse*10 ??
    Why reverse =reverse +number%10 ??
    Why number =number/10

    ReplyDelete
    Replies
    1. Yash Brahmani Kindly Dry Run The Code On a Note Book With A Simple input e.g 123
      Hope You Will Get The Logic If you face any Difficulty Ask Here Again.

      Delete
    2. How to make this program using array

      Delete
    3. you can do this programme using a string.

      Delete
  2. Replies
    1. I know where you lost it.
      Did you think 123%10=12?
      It's 3.
      I thought like that..:-p

      Delete
  3. it does not work when you enter 1000 in input

    ReplyDelete
    Replies
    1. Husnain Ali Shah.
      The Number You Get After Reverse 1000 is 1 It discard the 000 and Give The Actual Number in
      1... 1000 -> 0001 = 1

      Delete
  4. 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.

    ReplyDelete
  5. i 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.

    ReplyDelete
  6. Thank You very much . it works!

    ReplyDelete
  7. C++ Program to Reverse a Number

    Reverse of number means reverse the position of all digits of any number. For example reverse of 536 is 635

    ReplyDelete
  8. C++ Program to Reverse a Number

    Reverse of number means reverse the position of all digits of any number. For example reverse of 536 is 635

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. how to declare array through a variable?

    int 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

    ReplyDelete
  11. why the hell is it showing erroor in using namespace std??? it shows decleration syntax

    ReplyDelete
    Replies
    1. probably your software doesnt contain the namespace compiler

      Delete
  12. this 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

    ReplyDelete
  13. Omg thank you so much this is actually smart !

    ReplyDelete
  14. But 6 digit number cannot be reversed

    ReplyDelete