Recap - Product Array
Part of the Fundamentals section of Coddy's Java journey — lesson 62 of 73.
Challenge
EasyWrite a method named prod which gets an array of numbers as argument and returns the product of all the numbers in the list.
Reminder, product is the multiplication of all the numbers, for [1, 2, 3], return 6 = 1 * 2 * 3.
Try it yourself
import java.util.Scanner;
public class Main {
public static int prod(int[] arr) {
// Write your code below
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
String[] stringArr = text.split(",");
int[] arr = new int[stringArr.length];
for (int i = 0; i < stringArr.length; i++) {
arr[i] = Integer.parseInt(stringArr[i]);
}
int result = prod(arr);
System.out.println("Product of array elements: " + result);
}
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 411Arrays Basics
Declaring ArraysAccessing ElementsModifying ArraysArray MethodsRecap - Product ArrayRecap - Reversed Array3Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else