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++.
#include <iostream> /*included to allow for cout/cin to be used*/
#include <cmath>
usingnamespace 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;
}
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.