Menu
Coddy logo textTech

Recap - Product Array

Part of the Fundamentals section of Coddy's Java journey — lesson 62 of 73.

challenge icon

Challenge

Easy

Write 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