Below is my code so far that ask user for how many numbers they want to input. This program can calculate average based on the numbers that the user input but I also need it to find the product or multiplication of all the numbers entered. I know how to do if I set the number size but since the user deteremines that I don't know how to do the multiplication.

``````#include<iostream>
using namespace std;

int p, N = 0.0, sum =0;
float average = 0.0;

int main(){
cout<<"Enter the number of integers you want to enter: ";
cin>>N;
for (int i=0; i<N; i++) {
cout<<"enter a number: ";
cin>>p;
sum += p;

}
cout<<"The sum is "<<sum<<"\nThe average is "<<sum/N<<"\n";

system("pause");
}
``````
2
Contributors
4
Replies
20
Views
4 Years
Discussion Span
Last Post by tessa.burkhalterblackmon

Hi Tessa, welcome to DaniWeb!
You are doing integer division with sum/N, so example: if sum = 3, N = 2, average will be 1.0 and not 1,5 as it should.
For multiplication use the *= operator
Be aware of overflow

Can you help me add the code for that to my code? I tried but I'm getting 0 for the multiplication.

Edited by tessa.burkhalterblackmon

Define sum as float to avoid integer division.
Add `float product = 1;`
Add `product *= p;` to your for loop.
Use product also in cout.
Success.

Thank you! I did that but instead of using product I had multiplication and it didn't like it I guess. It works now. Thank you again for the quick response.