Even summation with recursive function in c++

In summary: 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
NewDesigner
1
0
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.
 
  • #3


Hi there,

It looks like you have made some good progress on this program! However, there are a few changes that need to be made in order for it to function properly. First, in your main function, you need to assign the return value of the sum() function to a variable so that you can print it out later. Additionally, you only need to ask the user to enter a number once in the main function, and then call the sum() function recursively to get the sum of all even numbers entered.

In the sum() function, you should have a base case that stops the recursion when the user has entered all the numbers. Then, you can check if the number entered is even or odd and add it to the appropriate sum variable. Finally, you can return the sum of even numbers and print it out in the main function.

Here is an example of how your code could look like:

#include<iostream>

using namespace std;

int sum(int);

int main()
{
int n;
int evenSum;

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

evenSum = sum(n);

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

return 0;
}

int sum(int n)
{
int num;
int evenSum = 0;

if (n == 0) // base case
{
return evenSum;
}
else
{
cout << "Enter number: ";
cin >> num;

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

return evenSum + sum(n-1); // recursive call with n-1
}
}

I hope this helps! Keep practicing and don't hesitate to ask for help if you get stuck. Good luck with your programming journey!
 

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.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
30
Views
2K
Back
Top