Reverse of number

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

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
  #11
Nov 3rd, 2009
By using this logic you can reverse only integer typed string...
  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. }
Now suppose I want to reverse this string "dkalita" ...
then we can't use this logic...then it will be better to use "strrev()" function...
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
  #12
Nov 3rd, 2009
Originally Posted by dkalita View Post
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.
Thanks a lot. So, is the below code correct.

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }
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
  #13
Nov 3rd, 2009
I am not ignoring dkalita's solution..
ya, its right and perfect....
but Mr tojan you just try to use this logic in char string "tojan"...
obiasly it will not work...
dkalita had ignored me to use "strlen()" function and told that his given solution is best ....
so, i mean to say only that dkalita's logic will work for only integer numbers...
and our C compilers are giving use ready mate functions to make better and easy programing then why we can't use this..???
hope you got it.....!!!
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
  #14
Nov 3rd, 2009
u code is not correct also.

7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }

This part pick-up every digit from an integer,so it is enough to complete the question.
printf("%d", x) is redundant . Think it over.
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
  #15
Nov 3rd, 2009
Originally Posted by nirav99 View Post
I am not ignoring dkalita's solution..
ya, its right and perfect....
but Mr tojan you just try to use this logic in char string "tojan"...
obiasly it will not work...
dkalita had ignored me to use "strlen()" function and told that his given solution is best ....
so, i mean to say only that dkalita's logic will work for only integer numbers...
and our C compilers are giving use ready mate functions to make better and easy programing then why we can't use this..???
hope you got it.....!!!
yep buddy.
You are right.
Its good to use ready made stuffs.
But when it comes to efficiency and all then sometimes its better to build some modules yourself.
That will improve your programming skills.
You will get to learn a lot.
When you feel that you have learned a lot, you will know when to use ready made stuffs and when not to.
It just depends on the requirement soimetimes.

EDIT:

The solution I gave is a generalized one which u can implement in any programming language where there might not be a library method for that.
N.B. And ofcourse my solution is only for integer.
Last edited by dkalita; Nov 3rd, 2009 at 2:14 am.
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
  #16
Nov 3rd, 2009
Thanks to nirav99.
:-) I think ill.
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
  #17
Nov 3rd, 2009
Originally Posted by seo2005 View Post
Thanks a lot. So, is the below code correct.

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }

y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);

in the above code,

y=3/10=0 thats o.k but

what is 3%10 ?

how will z becomes 3 as explained by dkalita
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
  #18
Nov 3rd, 2009
Originally Posted by seo2005 View Post
Thanks a lot. So, is the below code correct.

1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. int x,y,z;
6. scanf("%d",&x);
7. while(x>0)
8. {
9. y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);
13. }
14. printf("%d",x);
15. }
Hope u got your answer from Tojan Wong's reply.
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
  #19
Nov 3rd, 2009
hello, Mr Tojan..
i had run this program okay..
its works perfectly...
Better that you also run it....
don't use your mind if you not having enough thinking level...
so, first run this program then make your arguments okay...
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
  #20
Nov 3rd, 2009
Originally Posted by seo2005 View Post
y=x/10;
10 z=x%10;
11. x=y;
12. printf("%d",z);

in the above code,

y=3/10=0 thats o.k but

what is 3%10 ?

how will z becomes 3 as explained by dkalita
It would be better if you read about arithmatic operations in C first.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 873 | 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