| | |
Reverse of number
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
Hi,
This has been discussed ealier, but i would like to know how the loop executes
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. if(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }
Suppose I enter 321
y will become 32
z will become 1
x will be 32
It will print first z which is 1
then it will print x which is 32
But it should have been 123 ( Reverse of 321)
This has been discussed ealier, but i would like to know how the loop executes
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. if(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }
Suppose I enter 321
y will become 32
z will become 1
x will be 32
It will print first z which is 1
then it will print x which is 32
But it should have been 123 ( Reverse of 321)
0
#2 Nov 2nd, 2009
•
•
•
•
Suppose I enter 321
y will become 32
z will become 1
x will be 32
It will print first z which is 1
then it will print x which is 32
But it should have been 123 ( Reverse of 321)
See the code.
C Syntax (Toggle Plain Text)
if(x>0) { y=x/10; z=x%10; x=y; printf("%d",z); }
u wil have
y=x/10 i.e. y=32/10 = 3
z=x%10 i.e. z = 32%10 = 2
x=y i.e. x = 3
print z: 2 wil be printed now. AND SO ON untill x>0.
Why have u added the statement:
C Syntax (Toggle Plain Text)
printf("%d",x);
Last edited by dkalita; Nov 2nd, 2009 at 3:09 am.
•
•
Join Date: Jun 2009
Posts: 26
Reputation:
Solved Threads: 1
hi..
•
•
•
•
you just take while loop instead of if loop and store your result in array....
okay...just try this one...C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main() { int x,y,*z,i=0,j; clrscr(); scanf("%d",&x); while(x>0) { y=x%10; z[i]=y; x=x/10; printf("%d\n",z[i]); i++; } for(j=0;j<i;j++) { printf("%d",z[j]); } getch(); }
•
•
Join Date: Jun 2009
Posts: 26
Reputation:
Solved Threads: 1
0
#4 Nov 2nd, 2009
you can also do this using "strrev" function...
just try it also...
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char *x; gets(x); strrev(x); puts(x); getch(); }
just try it also...
0
#5 Nov 2nd, 2009
best way to do it is
C Syntax (Toggle Plain Text)
int reverse(int num) { int rev = 0; while(num>0) { rev = rev*10 + num%10; num = num/10; } return rev; }
Last edited by dkalita; Nov 2nd, 2009 at 7:39 am.
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
0
#6 Nov 2nd, 2009
•
•
•
•
why wil it print 32.
See the code.
the 32 wil again be processed similarly.C Syntax (Toggle Plain Text)
if(x>0) { y=x/10; z=x%10; x=y; printf("%d",z); }
u wil have
y=x/10 i.e. y=32/10 = 3
z=x%10 i.e. z = 32%10 = 2
x=y i.e. x = 3
print z: 2 wil be printed now. AND SO ON untill x>0.
Why have u added the statement:
It wil add an unwanted zero at the end of every reversed number.C Syntax (Toggle Plain Text)
printf("%d",x);
I understood upto this point -
y=x/10 i.e. y=32/10 = 3
z=x%10 i.e. z = 32%10 = 2
x=y i.e. x = 3
print z: 2 wil be printed now. AND SO ON untill x>0
Now x=3, so,
y=x/10 will be what ? I am not able to understand this.
0
#7 Nov 2nd, 2009
what do u expect y to be when x is 3.
y=x/10 = 3/10 = 0. thats it. Hence after printing z (which is now 3).
OK I thought u wrote while(x>0). I ignored that line. Sorry for the misunderstanding.
If it were a while loop then next time x becomes zero and the loop breaks.
BTW I dont understand why u have used if(x>0) that wil consider for only one digit.
U must use a while loop instead.
y=x/10 = 3/10 = 0. thats it. Hence after printing z (which is now 3).
OK I thought u wrote while(x>0). I ignored that line. Sorry for the misunderstanding.
If it were a while loop then next time x becomes zero and the loop breaks.
BTW I dont understand why u have used if(x>0) that wil consider for only one digit.
U must use a while loop instead.
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
•
•
•
•
y=x/10 = 3/10 = 0. thats it
Thanks
![]() |
Similar Threads
- how to reverse a numbers input by user (C)
- Reverse Digits (C)
- How do I reverse numbers in my program (C++)
- Reversing a number (Java)
- how to display a number's reversal (C++)
- reversing number problem. (C)
- function call to determine a palindrome number (C++)
Other Threads in the C Forum
- Previous Thread: Reading and editing characters in a string
- Next Thread: pointers and malloc help needed
Views: 888 | Replies: 23
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student systemcall test testautomation unix user variable visualstudio voidmain() wab win32 win32api windows.h





