| | |
reversing number problem.
![]() |
•
•
Join Date: Jul 2004
Posts: 65
Reputation:
Solved Threads: 0
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....
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....
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.
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.
•
•
•
•
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.
C Syntax (Toggle Plain Text)
#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; }
•
•
•
•
Originally Posted by Mr.PinkBunny
I don't under stand the part with the remainder. What does this part do?
Then the remainder is taken again and the next digit can be stored
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
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.
C Syntax (Toggle Plain Text)
#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); } }
![]() |
Similar Threads
- Reputed number problem switch case statement (VB.NET)
- Auto number problem (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: need help!!! about fibonacci
- Next Thread: Calendar Program
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency function givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





