| | |
Reverse of number
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 26
Reputation:
Solved Threads: 1
0
#11 Nov 3rd, 2009
By using this logic you can reverse only integer typed string...
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...
•
•
•
•
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; }
then we can't use this logic...then it will be better to use "strrev()" function...
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
•
•
•
•
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.
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. }
•
•
Join Date: Jun 2009
Posts: 26
Reputation:
Solved Threads: 1
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.....!!!
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.....!!!
0
#15 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.....!!!
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.
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 0
0
#17 Nov 3rd, 2009
•
•
•
•
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
![]() |
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: 873 | Replies: 23
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user visualstudio voidmain() wab win32 win32api windows.h





