reversing number problem.

Closed Thread

Join Date: Jul 2004
Posts: 65
Reputation: koh is an unknown quantity at this point 
Solved Threads: 0
koh koh is offline Offline
Junior Poster in Training

reversing number problem.

 
0
  #1
Jul 27th, 2004
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 <iostream>
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....
Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: reversing number problem.

 
0
  #2
Jul 27th, 2004
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.
Quick reply to this message  
Join Date: Jul 2004
Posts: 7
Reputation: Kalitko is an unknown quantity at this point 
Solved Threads: 0
Kalitko Kalitko is offline Offline
Newbie Poster

Re: reversing number problem.

 
0
  #3
Jul 28th, 2004
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.
Quick reply to this message  
Join Date: Jul 2004
Posts: 16
Reputation: rishiraj_bayerd is an unknown quantity at this point 
Solved Threads: 0
rishiraj_bayerd rishiraj_bayerd is offline Offline
Newbie Poster

Re: reversing number problem.

 
0
  #4
Jul 31st, 2004
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
Quick reply to this message  
Join Date: Aug 2004
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

Re: reversing number problem.

 
0
  #5
Sep 4th, 2004
Originally Posted by Chainsaw
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, but int arrays, not strings, which I think are easier to manipulate.

  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6. int a[5]={0,0}; //5 because max value of int is 32767
  7. int no, size=0;
  8. cin>>no;
  9. int r, t, i;
  10. t=no;
  11. while (no) //for claculating the no of digits
  12. {
  13. r=no%10;
  14. no/=10;
  15. size++;
  16. }
  17. while (t) //storing digits in the array
  18. {
  19. r=t%10;
  20. t/=10;
  21. a[i]=r;
  22. i++;
  23. }
  24. for (i=0; i<size; i++) //display
  25. cout<<a[i];
  26. getch();
  27. return 0;
  28. }
Quick reply to this message  
Join Date: Jul 2004
Posts: 8
Reputation: Mr.PinkBunny is an unknown quantity at this point 
Solved Threads: 0
Mr.PinkBunny's Avatar
Mr.PinkBunny Mr.PinkBunny is offline Offline
Newbie Poster

Re: reversing number problem.

 
0
  #6
Sep 4th, 2004
I don't under stand the part with the remainder. What does this part do?

r=no%10;
:evil::cry::evil::cry::evil::cry::evil::cry::evil:
Quick reply to this message  
Join Date: Aug 2004
Posts: 24
Reputation: XianBin is an unknown quantity at this point 
Solved Threads: 0
XianBin XianBin is offline Offline
Newbie Poster

Re: reversing number problem.

 
0
  #7
Sep 4th, 2004
r = no % 100;
==================
no = 120;
120 % 100 = 20;
130 % 100 = 30;
100 % 100 = 0;

I dont konw how to describle it in Eglish.
Quick reply to this message  
Join Date: Aug 2004
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

Re: reversing number problem.

 
0
  #8
Sep 5th, 2004
Originally Posted by Mr.PinkBunny
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
Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: kamlesh_ag07 is an unknown quantity at this point 
Solved Threads: 0
kamlesh_ag07 kamlesh_ag07 is offline Offline
Newbie Poster

Re: reversing number problem.

 
0
  #9
Oct 9th, 2007
This program gets very easy with the use if recursive function if the job is only to print the number in the reverse order.


  1.  
  2. #include <iostream>
  3.  
  4. #include <conio>
  5.  
  6. void rev(int);
  7.  
  8. void main()
  9. {
  10. int a;
  11.  
  12. cin >> a;
  13.  
  14. rev(a);
  15.  
  16. getch();
  17. }
  18.  
  19. void rev(int a)
  20. {
  21. if(a)
  22. {
  23. cout << a%10;
  24. rev(a/10);
  25. }
  26. }
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: reversing number problem.

 
0
  #10
Oct 9th, 2007
>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?
I'm here to prove you wrong.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC