Even summation with recursive function in c++

Click For Summary
SUMMARY

The discussion focuses on implementing a C++ program that calculates the sum of even numbers using a recursive function. The user inputs a number 'n' to specify how many integers they will enter. The main function calls a recursive function, 'sum', which reads the numbers and computes the sum of even integers. Key issues identified include the need for a stopping condition in recursion and the proper handling of variables to retain their values across function calls.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Knowledge of recursion in programming
  • Familiarity with conditional statements in C++
  • Basic input/output operations in C++ using cin and cout
NEXT STEPS
  • Learn about recursion depth and base cases in C++ functions
  • Explore variable scope and lifetime in C++
  • Study how to pass parameters by reference in C++ functions
  • Investigate error handling and input validation in C++
USEFUL FOR

Beginner C++ programmers, computer science students, and anyone interested in mastering recursive functions and input handling in C++.

NewDesigner
Messages
1
Reaction score
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;

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

	sum(n);

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

	return 0;
}

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

	count << "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
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 39 ·
2
Replies
39
Views
5K
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
12
Views
3K