Question

7. Write a recursive method (sum) that uses the following formula to find the sum Your answer:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code:

#include<stdio.h>
int sum(int);
int main()
{
   int n,result;       //variable declaration
   printf("\nEnter the value of N: ");
   scanf("%d",&n);       //takes input
   result=sum(n);       //call recursive function
   printf("sum of 1 to %d is %d\n",n,result);       //print result
   return 0;
}
//Resursive function
int sum(int n)
{
   /* In every recursive call value of n is decremented by 1
       and when the value of n becomes 1 it returns 1 */
   if(n==1)  
   {
       return 1;
   }
   else
   {
       return n+sum(n-1);
   }
}

Output:

-/Desktop/Chegg/sum.c - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Help sum.c o

Add a comment
Know the answer?
Add Answer to:
7. Write a recursive method (sum) that uses the following formula to find the sum Your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT