Reverse of number

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster

Reverse of number

 
0
  #1
Nov 2nd, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
Nov 2nd, 2009
Originally Posted by seo2005 View Post
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)
why wil it print 32.
See the code.
  1. if(x>0)
  2. {
  3. y=x/10;
  4. z=x%10;
  5. x=y;
  6. printf("%d",z);
  7. }
the 32 wil again be processed similarly.
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:
  1. printf("%d",x);
It wil add an unwanted zero at the end of every reversed number.
Last edited by dkalita; Nov 2nd, 2009 at 3:09 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 26
Reputation: nirav99 has a little shameless behaviour in the past 
Solved Threads: 1
nirav99 nirav99 is offline Offline
Light Poster

hi

 
0
  #3
Nov 2nd, 2009
hi..
you just take while loop instead of if loop and store your result in array....
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int x,y,*z,i=0,j;
  6. clrscr();
  7. scanf("%d",&x);
  8. while(x>0)
  9. {
  10. y=x%10;
  11. z[i]=y;
  12. x=x/10;
  13. printf("%d\n",z[i]);
  14. i++;
  15. }
  16. for(j=0;j<i;j++)
  17. {
  18. printf("%d",z[j]);
  19. }
  20. getch();
  21. }
okay...just try this one...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 26
Reputation: nirav99 has a little shameless behaviour in the past 
Solved Threads: 1
nirav99 nirav99 is offline Offline
Light Poster
 
0
  #4
Nov 2nd, 2009
you can also do this using "strrev" function...
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. void main()
  5. {
  6. char *x;
  7.  
  8. gets(x);
  9. strrev(x);
  10. puts(x);
  11.  
  12. getch();
  13. }

just try it also...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #5
Nov 2nd, 2009
best way to do it is
  1. int reverse(int num)
  2. {
  3. int rev = 0;
  4. while(num>0)
  5. {
  6. rev = rev*10 + num%10;
  7. num = num/10;
  8. }
  9. return rev;
  10. }
Last edited by dkalita; Nov 2nd, 2009 at 7:39 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster
 
0
  #6
Nov 2nd, 2009
Originally Posted by dkalita View Post
why wil it print 32.
See the code.
  1. if(x>0)
  2. {
  3. y=x/10;
  4. z=x%10;
  5. x=y;
  6. printf("%d",z);
  7. }
the 32 wil again be processed similarly.
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:
  1. printf("%d",x);
It wil add an unwanted zero at the end of every reversed number.

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster

Reverse of number

 
0
  #8
Nov 3rd, 2009
y=x/10 = 3/10 = 0. thats it
I was not sure about this. You mean to say, that in C programming, division of two dissimilar whole numbers proceeds like this. I will be very grateful if you can throw more light on this or point to a helpful tutorial where I can understand this. I am beginining to learn C a bit. So far you have been very helpful.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #9
Nov 3rd, 2009
when u divide an integer by an integer u get the result also an integer.
generally when u do 3/2 it should have been 1.5 but since both are integer values u will get the result as 1 not 1.5.
If u want 1.5 u have to typecast either of the operands to float.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: Tajon Wong is an unknown quantity at this point 
Solved Threads: 0
Tajon Wong Tajon Wong is offline Offline
Newbie Poster
 
0
  #10
Nov 3rd, 2009
dkalita's solution is perfect!
I get it.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 888 | Replies: 23
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC