Copyright © Programs ++
Design by Dzignine
Tuesday 17 April 2012

Reverse of an integer in C++

The modulus operator (%) is widely used in many programming languages, especially in C/C++. It returns the remainder. For e.g 10%2 returns 0 as the remainder that we get when dividing 10 by 2 is 0.

One use of this operator is used for finding the reverse of an integer. Simply divide the integer by 10 and use the modulus operator to store
the remainder. The following programs demonstrates the above concept.
 
// Program to find out the reverse of an integer

#include <iostream>

int main()
{
     unsigned int x;
     // prompts the user to enter an integer
     std::cout<<" Enter the number : ";
     std::cin>>x;
     std::cout<<" The reverse of "<<x<<" is : ";
     while ( x != 0)
     {
          std::cout<<x%10;
          x = x/10;
     }
     return 0;
 }// end of main

------ OUTPUT ------











Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.

0 comments:

Post a Comment

Comment Here....