finding product

hi!
i wanna find the product of all inputs that user enter
and also user determine the length of an array
when the array size is 1000 the final product for each input is 0
and i don't know why is that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include<iostream>
using namespace std;
int main()
{
	int ar[1000]={1},N=0,input=0,product=1;
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cin>>input;
		if(input>0)
		{
		  ar[i]=input;
		  product*=ar[i];
		}
		
	}
	  cout<<product<<endl;
	return 0;
}
Well let's start with the obvious: are any of those 1,000 numbers zero? If so then the product will be zero.

If not then you are probably overflowing the capacity of type int. Int is typically a 32-bit signed number and so has a maximum value that's a little more than 2 billion. Unless your numbers are very small and consist mostly of 1's, the product of any 1,000 numbers will probably be larger than 2 billion.

Try copying line 17 to line just after line 13 so you print out the intermediate products. That should help you see what's going on.

If you really need to multiply so many numbers then consider making product a double or long double.
Also consider using a standard library container like std::array or std::vector and applying std::multiplies:
http://en.cppreference.com/w/cpp/utility/functional/multiplies
i can't use double it should be just int

and it doesn't accept 0 because if(input>0)

i copied line 17 after line13 every thing went ok by this input:
5
1 2 3 4 5
out:
1
2
6
24
120
even i changed the int to long long int
but doesn't work:<
i can't get point
why 1000???????
Last edited on
Topic archived. No new replies allowed.