Question

how to write a recursion function that takes an int (i) and returns the sum of...

how to write a recursion function that takes an int (i) and returns the sum of the integers from 1 to to i and print sum with C.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
int sumofnumbers(int i);
int main()
{
int num;
printf("Enter a postive integer: ");
scanf("%d", &num);
printf("sum = %d",sumofnumbers(num));
return 0;
}
int sumofnumbers(int i)
{
if(i!=0)
return i +sumofnumbers(i-1);
else
return i;
}

output::

​​​​​​

Add a comment
Know the answer?
Add Answer to:
how to write a recursion function that takes an int (i) and returns the sum of...
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
  • Write a function that takes an int as an argument and returns true if the int...

    Write a function that takes an int as an argument and returns true if the int is an even number and false if not. In any case change the value of the original argument to twice its value. Use the integers 0 through 4 as test cases. Do not use any arrays.

  • Write a short C++ function, isTwoPower, that takes an int i and returns true if and...

    Write a short C++ function, isTwoPower, that takes an int i and returns true if and only if i is a power of 2. Do not use multiplication or division,

  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key:...

    USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key: int, value: int) Description: You're done with stats, but you still have other math homework to go! You're currently learning about the Fibonacci sequence in math class. The Fibonacci sequence is a series of numbers in the pattern 112 3 5 8 13 21 ..., where the next number is found by adding up the two numbers before it. Write a function that takes...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Using C Write the function: long sum (long *a, long n); This function takes an array...

    Using C Write the function: long sum (long *a, long n); This function takes an array a and an integer n, and returns the sum of the n first long integers in a. You are not allowed to use the notation a[i], you must use pointer arithmetic.

  • Write a recursive function sum-odds that takes a non-empty list of integers

    in python Part I: Sum of Odd Integers Write a recursive function sum-odds that takes a non-empty list of integers as an argument and returns the sum of only the odd integers in the list. In class we explored a recursive function called rsum that recursively computes the sum of a list of integers use it as a model to get started. Your function must be recursive and must not use any loops

  • How can I make this into a recursive function (C++)?? //Write a function that, given positive...

    How can I make this into a recursive function (C++)?? //Write a function that, given positive integers 1 <= n <= 10^4 and 1 <= s <= n as input, returns the sum of the last s elements in the sequence from 1 to n (inclusive). unsigned long int suffix_sum(unsigned int n, unsigned int s){ unsigned long int sum = 0,r=(n-s)+1; while (r<=n){ sum = sum + r; r++; } return sum; return 0; }

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

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