943,548 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11927
  • C RSS
Jul 27th, 2004
0

reversing number problem.

Expand Post »
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....
Similar Threads
koh
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
koh is offline Offline
73 posts
since Jul 2004
Jul 27th, 2004
0

Re: reversing number problem.

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jul 28th, 2004
0

Re: reversing number problem.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kalitko is offline Offline
7 posts
since Jul 2004
Jul 31st, 2004
0

Re: reversing number problem.

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
Reputation Points: 13
Solved Threads: 0
Newbie Poster
rishiraj_bayerd is offline Offline
16 posts
since Jul 2004
Sep 4th, 2004
0

Re: reversing number problem.

Quote 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. }
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Sep 4th, 2004
0

Re: reversing number problem.

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

Quote ...
r=no%10;
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Mr.PinkBunny is offline Offline
8 posts
since Jul 2004
Sep 4th, 2004
0

Re: reversing number problem.

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

I dont konw how to describle it in Eglish.
Reputation Points: 15
Solved Threads: 0
Newbie Poster
XianBin is offline Offline
24 posts
since Aug 2004
Sep 5th, 2004
0

Re: reversing number problem.

Quote 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
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Oct 9th, 2007
0

Re: reversing number problem.

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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kamlesh_ag07 is offline Offline
6 posts
since Oct 2007
Oct 9th, 2007
0

Re: reversing number problem.

>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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: need help!!! about fibonacci
Next Thread in C Forum Timeline: Calendar Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC