javascript - Multiplying issue in algorithm -
i'm still new algorithms. but, think i'm getting hand of it. however, in code, i'm having trouble last part of program. specifically, i'm trying write function multiplies numbers , outputs result. `
var prod=0; prod=0; for(i=0;i<n;i++) { prod=prod*a[i]; } document.write("product= "+prod+"<br>");
`
i've looked everywhere solve question.
full code:
var i,n; var max=0; var min=0; var sum=0; var avg=0; var prod=0; n=parseint(prompt("enter array size")); var a=new array(n); for(i=0;i<n;i++) { a[i]=parseint(prompt("enter numbers")); } // determine number greatest max=a[0]; for(i=1;i<n;i++) { if(max<a[i]) max=a[i]; } // determine number least min=a[0]; for(i=1;i<n;i++) { if(min>a[i]) min=a[i]; } // determine sum of numbers sum=0; for(i=0;i<n;i++) { sum=sum+a[i]; } // determine mean of numbers avg=sum/n; document.write("array elements : – "); for(i=0;i<n;i++) { document.write(a[i]*" "); } // determine product of numbers prod=0; for(i=0;i<n;i++) { prod=prod*a[i]; } document.write("<br>"+"maximum= "+max+"<br>"); document.write("minimum number= "+min+"<br>"); document.write("sum of array numbers= "+sum+"<br>"); document.write("average= "+avg+"<br>"); document.write("product= "+prod+"<br>");
thanks in advance
so, you're saying prod=prod*a[i];
when initial value of prod 0. change 1 , you're go.
Comments
Post a Comment