Summation Program.

I'm supposed to be making a program that can find the summation of a function (such as x^5+10) from x=1 to n, which is inputted by the user. I'm supposed to do this once using only "for" loops, and once using only "while" loops. But so far, I don't even know how to get that far. This is what I have, but it doesn't do anything, primarily because I have no idea how to do a summation on C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>  /*included to allow for cout/cin to be used*/
#include <cmath>
using namespace std;
int main() {
	int cont;
	int n;
	int sum = 0;
	int number;
	
	cout << "Do you want to find a summation? (Y/N) \n";
	cin >> cont;
	int N;
	
	if (cont == N) {
	return 0;
	}
	
	cout << "Enter an n value. n= ";
	cin >> n;
	
	

	//int func = (pow(x,5)+10);
	




return 0;
}


Thanks for any help.
Last edited on
You just need to use a loop that runs from 1 to n (the user's input). Inside the loop, find the value of the function at that value for X (1 through n) and add it to sum. For instance, in the function you gave, it would be something like
 
sum += pow(i, 5) + 10;


Start with doing this in a for loop, then changing it to a while loop should be trivial.
Last edited on
Topic archived. No new replies allowed.