Hi,(adsbygoogle = window.adsbygoogle || []).push({});

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 any body 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;

}

**Physics Forums - The Fusion of Science and Community**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Even summation with recursive function in c++

Loading...

Similar Threads for summation recursive function | Date |
---|---|

Java Recursive tracing java | Apr 4, 2017 |

Java Sudoku Solver Problem | Mar 30, 2017 |

Parallel Kahan summation | Oct 14, 2013 |

Summation function to minimize rounding issues | Apr 29, 2013 |

Problem with summation algorithm | Sep 29, 2007 |

**Physics Forums - The Fusion of Science and Community**