How to calculate the product of multiplication of the elements of an array of numbers?

Asked

Viewed 229 times

-1

How to go through an array of numbers and calculate the multiplication result of all their elements?

For example, with [1, 4, 7] get 28, which is 1 * 4 * 7.

1 answer

1

Welcome to the Sopt!

Well, considering the level of the question you presented, I believe you should be learning the bond for correct? I will leave here a solution that I believe is more in line with your line of studies, but there are better solutions for this situation :)

int arrayNumeros[] = {1,4,7};
int total = 1;

for (int i = 0; i < arrayNumeros.length; i++) 
{
   total = arrayNumeros[i] * total;
}
System.out.println(total);

Note that I entered both the total variable and the INT type array because in your example you only use integers, if you want to work with decimal numbers you must change the two to the FLOAT type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.