954,224 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reversing number problem.

i have done a simple program, which i asked..(don't know how many days ago.)

but i find that there is a problem..

if i give an input (for example) 1234

it shall reverse back 4321 (it does)

when i key in 100, by right it should return 001, but it returned as 1.

my question is how to make that two zeros in front visible. (is there any mistake(s) for my "this" program??)

#include
using namespace std;

long reverse();

int main()
{ long a;
a = reverse();
cout << "The reversed number is " << a << endl;

return 0;
}

long reverse()
{
long x, y, z = 0;

cout << "Enter an integer to be reversed: ";
cin >> x;
while (x > 0){
y = x%10;
x = x/10;
z = 10 * z + y;
}
return z;
}

thank you....

koh
Junior Poster in Training
73 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

The trouble here is you are using integers to reverse the number. That works for most things, but the number '1' could be '01' or '001' or '0001' and so on and you use a generic integer printing routine.

Two easy solutions I can think of here; one is to return how many digits the answer should be and make a string out of the result with leading zeroes (using sprintf(), say). the other is to do the reversal as a string of chars.

Both will take a small investment of time. The string reversal favors using the reverse() algorithm, whereas the sprintf is more of a clever formatting trick.

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

You should declear the number as a string or to convert it as a string. In your way it treat it as an integer and takes off the zeros of the front which don't mean anything for the compiler.

Kalitko
Newbie Poster
7 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

hi kho,
here you take input as a integer.The 001,00001,01,1 is same thing to the cout.
If you want to see output as 001 then you take input in a string.

thanking you.....
Rishi

rishiraj_bayerd
Newbie Poster
16 posts since Jul 2004
Reputation Points: 13
Solved Threads: 0
 
Two easy solutions I can think of here; one is to return how many digits the answer should be and make a string out of the result with leading zeroes (using sprintf(), say). the other is to do the reversal as a string of chars.


You do have to use arrays in the method I know, butint arrays, not strings, which I think are easier to manipulate.

#include <iostream.h>
#include <conio.h>

int main()
{
 int a[5]={0,0}; //5 because max value of int is 32767
 int no, size=0;
 cin>>no;
 int r, t, i;
 t=no;
 while (no)  //for claculating the no of digits
 {
  r=no%10;
  no/=10;
  size++;
 }
 while (t) //storing digits in the array
 {
  r=t%10;
  t/=10;
  a[i]=r;
  i++;
 }
 for (i=0; i<size; i++)  //display
 cout<<a[i];
 getch();
 return 0;
}
iamboredguy
Newbie Poster
23 posts since Aug 2004
Reputation Points: 12
Solved Threads: 0
 

I don't under stand the part with the remainder. What does this part do?

r=no%10;
Mr.PinkBunny
Newbie Poster
8 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

r = no % 100;
==================
no = 120;
120 % 100 = 20;
130 % 100 = 30;
100 % 100 = 0;

I dont konw how to describle it in Eglish.

XianBin
Newbie Poster
24 posts since Aug 2004
Reputation Points: 15
Solved Threads: 0
 
I don't under stand the part with the remainder. What does this part do?


The remainder gives you the individual digit. After the remainder is taken, the no. is divided by 10. Since in int no decimal pts are allowed, the original no. has one digit less.
Then the remainder is taken again and the next digit can be stored

iamboredguy
Newbie Poster
23 posts since Aug 2004
Reputation Points: 12
Solved Threads: 0
 

This program gets very easy with the use if recursive function if the job is only to print the number in the reverse order.

#include <iostream>
 
 #include <conio>

 void rev(int); 

 void main()
 {
        int a;

       cin >> a;
     
       rev(a);

      getch();
 }

 void rev(int a)
 {
        if(a)
        {
               cout << a%10;
               rev(a/10);
         }
 }
kamlesh_ag07
Newbie Poster
6 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

>This program gets very easy with the use if recursive
>function if the job is only to print the number in the reverse order.
So, was it worth resurrecting a three year old thread to post some bad code?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You