I'm trying to create a program that will sum all of the numbers from 1 to n, which the user inputs. I have to use recursion to calculate the sum. For example if the user inputs 5 it will return the sum of 1,2,3,4,5.
Here is what I have so far:
#include <iostream>
using namespace std;
int sum(int);
int main()
{
int number;
cout << "Enter an integer: ";
(cin >> number).get();
cout << "The sum of the numbers up to " << number << " is " << sum << endl;
cin.get();
return 0;
}
int sum(int n)
{
if (n == 1)
return 1;
else
return n + sum(n + 1);
}
I'm having problems with the recursion calculation. It is giving me extraordinarily large answers. Can anyone help?