Even summation with recursive function in c++

In your main function, call the recursive function sum and assign the returned value to a variable, say sumEven. Then print the value of sumEven.In summary, the conversation is about a person asking for help in writing a program in C++ that asks the user to enter a certain number of numbers and calculates the sum of even numbers only. The program uses a recursive function to read the values entered by the user and returns the summation of even numbers. The conversation also includes some hints on how to solve the problem correctly.
  • #1
Hi,

I'm a beginner in C++.
I wan't to write this program:

Write a program that asks the user to enter n numbers –where n entered by the user- and calculates the sum of even numbers only. main function asks the user to enter n and then calls the recursive function Sum to read the values entered by the user. The function Sum returns the summation of even numbers only. At the end, main prints the summation that is returned by the recursive function.

Sample run:

How many numbers do you have:? 6
Enter number:3
Enter number:2
Enter number:9
Enter number:5
Enter number:6
Enter number:8
Sum of odd numbers = 16

I solve this question in that way, and i know it's wrong. can anybody help me please?

PHP:
#include<iostream>

using namespace std;

double sum(int);

int main()
{
	int n;

	cout << "How many numbers do you have? ";
	cin >> n;

	sum(n);

	cout << "Sum of even numbers = " << sumEven << endl;

	return 0;
}

double sum(int a)
{
	int num;
	double sumEven=0;
	double sumOdd=0;

	cout << "Enter number: ";
	cin >> num;

	if(num%2==0)
	{
		sumEven += num;
		sum(num);
	}

	else if(num%2==1)
	{
		sumOdd += num;
		sum(num);
	}
		
	return sumEven;
}
 
Technology news on Phys.org
  • #2
Few hints

1) Decrement your argument 'a' each time you get a number , otherwise the recursion executes infinitely. you must have a stopping condition.

2)Use sumEven (sumOdd also if you require sum of odd nos) as an argument in your function, rather than declaring it as a local variable. This way you will be able to use the same variable in each function (retaining previous value). Local declaration creates a new variable each time the function is called.
 

1. What is a recursive function in C++?

A recursive function in C++ is a function that calls itself repeatedly until a certain condition is met. It is a powerful programming technique that allows for solving problems through smaller sub-problems.

2. How does a recursive function work?

A recursive function works by breaking down a larger problem into smaller sub-problems. The function calls itself with smaller inputs until a base case is reached, which is the condition that stops the function from calling itself.

3. What is the purpose of using a recursive function in C++?

The purpose of using a recursive function in C++ is to simplify complex problems into smaller, more manageable sub-problems. It also allows for efficient use of memory and code reusability.

4. What is the time complexity of a recursive function in C++?

The time complexity of a recursive function in C++ depends on the number of times the function calls itself. In general, the time complexity can range from O(1) to O(n), with O(n) being the worst-case scenario.

5. Can a recursive function cause a stack overflow error?

Yes, a recursive function can cause a stack overflow error if the function calls itself too many times without reaching the base case. This can happen if the base case is not properly defined or if the function is not designed to handle large inputs.

Suggested for: Even summation with recursive function in c++

Replies
2
Views
761
Replies
39
Views
2K
Replies
2
Views
819
Replies
2
Views
923
Replies
3
Views
1K
Replies
16
Views
1K
Replies
6
Views
6K
Replies
11
Views
684
Replies
13
Views
943
Back
Top