ccplusplus.com
Learn C, C++ Concepts
Saturday, September 10, 2011
reverse number using recursion in c program
/****************************************************** * File : reverse-given-number-using-recursion.c * Author : Saurabh Gupta * Source : http://saurabhgupta0527.blogspot.com/ * Created : AM 11:29 10 September 2011 *****************************************************/ #include<stdio.h> #include <stdlib.h> #include <unistd.h> int reverse (int); int main(int argc, char** argv){ int nNumber,nReverse; printf("Enter a number : "); scanf("%d",&nNumber); nReverse = reverse(nNumber); printf("Number after reverse is : %d",nReverse); printf ("\n"); return 0; } int sum = 0, r; int reverse(int nNumber){ if(nNumber){ r = nNumber%10; sum = sum*10 + r; reverse(nNumber/10); } else { return sum; } return sum; } /* * OUTPUT * [sgupta@rhel54x64 c]$ [sgupta@rhel54x64 c]$ ./reverse-given-number-using-recursion Enter a number : 123 Number after reverse is : 321 [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
2 comments:
Anonymous
September 14, 2012 at 7:26 PM
good and nice
Reply
Delete
Replies
support@ccplusplus.com
September 14, 2012 at 11:21 PM
glad 2 hear that :)
Delete
Replies
Reply
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
good and nice
ReplyDeleteglad 2 hear that :)
Delete