multiply all numbers within range using for loop

Hi! I'm trying to write a code that uses a for loop that allows a user to input a number then displays the product of 1 and the number the user entered e.g. if they enter 5 then it'll calculate 1*2*3*4*5
#include <iostream>
#include <cmath>

using namespace std;
int main() {
double n;
double product;

cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i ) {
product = i * i;
cout << product << endl;

}

return 0;

This is what I have so far, i'm still confused on what to do.
}
Please use ["code"] and ["/code"] without the quotation marks and put your code inbetween them. As for your question, you're setting the product to i*i each time the loop runs, so if the user entered 5 for example, the end value of product will be 25. Also, n doesn't need to be double, it only needs to be an unsigned int, same goes for product. return 0; has to be in the main function.
Alright, so does that mean I need a statement that adds them all up?
No, that means you have to multiply product by i each time the loop runs. Move the line that outputs product out of the for loop. Make sure that product is equal to 1 before the loop begins. Also #include <cmath> is not needed for this program.
Ok, I think i've figured it out!

#include <iostream>

using namespace std;
int main() {
int n;
int product = 1;

cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
product = i * product;
}

{

cout << product << endl;

}
return 0;
}

So my problem was i was not multiplying by next number in the loop because the product wasn't initialized to anything, it was just an empty variable?

I tested it and it works, thank you for the help !
Topic archived. No new replies allowed.