ccplusplus.com
Learn C, C++ Concepts
Friday, September 16, 2011
sum of 1 to n numbers using recursion in c
/************************************************************* * File : sum-of-n-numbers-using-recursion.c * Author : Saurabh Gupta * Desc : sum of n numbers using recursion in c * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : PM 11:34 16 September 2011 *************************************************************/ #include<stdio.h> /* * function declaration */ int recursiveSum(int); int main () { int i, nNumber; int nSum; printf("Input a number : "); scanf("%d",&nNumber); nSum = recursiveSum(nNumber); printf("Sum of number's from 1 to %d is : %d",nNumber, nSum); printf ("\n"); } int recursiveSum(int nTempNumber) { int nTempSum; if(nTempNumber == 1) { return(1); } else { nTempSum = nTempNumber + recursiveSum (nTempNumber - 1); } return nTempSum; } /* * OUTPUT * [sgupta@rhel54x64 c]$ gcc sum-of-n-numbers-using-recursion.c -o sum-of-n-numbers-using-recursion [sgupta@rhel54x64 c]$ ./sum-of-n-numbers-using-recursion Input a number : 10 Sum of number's from 1 to 10 is : 55 [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment