C++:Reverse a number

Printer-friendly versionSend to friend

Upon request, i am posting a small program which would reverse a number.For an example we have:

Input 12345
Output 54321

Note:Please use long as data type else this program will no work for digits greater than four.

Here goes the program.

#include<iostream.h>
 
void main()
{
       cout<<"\t\t||Jai Saraswati||\n\n";
 
       long num,rem,newn=0;
       cout<<"Enter the Number:\t";
       cin>>num;
 
       while(num!=0)
       {
               rem=num%10;
               newn=rem+newn*10;
               num=num/10;
       }
 
       cout<<"Reverse Number is:\t";
       cout<<newn;
}
[/C++]

Comments

Another option also possible

I don't know much of C++, but we had to do this exercise in VB 6 too, so I used to convert LONG to String and then reverse the string because that was easier in VB.


Nilesh Govindrajan

Site & Server Administrator
iTech7

One thing i like most about

One thing i like most about programing is that unlike other subjects users are not bounded to do a task within the lines. We focus more on result rather than the way of doing it. So its all your wish how to solve a question...

Ontopic I have not tried but i don't think it will be user friendly to convert data types in C++.

Syndicate content