CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

print the product & square of each element

JAVA November 19, 2025 4 views 0 likes
Define a class to declare an array of size twenty of double datatype, accept the elements into the array and perform the following :

Calculate and print the product of all the elements.
Print the square of each element of the array.

Answer:

KboatSDADouble - Java Code

KboatSDADouble - Java Code


import java.util.Scanner;

public class KboatSDADouble
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        double arr[] = new double[20];
        int l = arr.length;
        double p = 1.0;
        
        System.out.println("Enter 20 numbers:");
        for (int i = 0; i < l; i++)
        {
            arr[i] = in.nextDouble();
        }
        
        for (int i = 0; i < l; i++)
        {
            p *= arr[i];
        }        
        System.out.println("Product = " + p);
        
        System.out.println("Square of array elements :");
        for (int i = 0; i < l; i++)
        {
            double sq = Math.pow(arr[i], 2);
            System.out.println(sq + " ");
        }  
    }
}

OUTPUT:

Kboat SDA Double - JS Version

Enter 20 Numbers

4 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285