0% found this document useful (0 votes)
2 views

Java Program print 20 numbers

The document describes a Java program that takes 20 numbers as input from the user, calculates their product, and displays the result. It includes explanations of key components such as imports, class declaration, main method, array declaration, input loop, product calculation, result display, and scanner closure. Overall, it outlines how the program operates to achieve its functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Program print 20 numbers

The document describes a Java program that takes 20 numbers as input from the user, calculates their product, and displays the result. It includes explanations of key components such as imports, class declaration, main method, array declaration, input loop, product calculation, result display, and scanner closure. Overall, it outlines how the program operates to achieve its functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JAVA PROGRAM THAT WILL PRINT THE PRODUCT OF

TWENTY NUMBERS

import java.util.Scanner;

public class TwentyNum {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

double[] numbers = new double[20];

System.out.println("Enter 20 numbers:");

// Taking input for 20 numbers


for (int i = 0; i < 20; i++) {
System.out.print("Enter number " + (i + 1) + ":");
numbers[i] = scanner.nextDouble();
}

double product = 1;

// Calculating product
for (double num : numbers) {
product *= num;
}

// Displaying the product


System.out.println("Product of the 20 numbers is: " +
product);

scanner.close();
}
}
THE ABOVE CODES MEAN THE FOLLOWING
1. Imports: import java.util.Scanner; This line imports the Scanner class from

the java.util package, which is used to read user input from the console.

2. Class Declaration: public class ProductOfTwentyNumbers { ... } This

line declares a class named ProductOfTwentyNumbers.

3. Main Method: public static void main(String[] args) { ... } This is

the entry point of the program. Execution of the code begins here.
4. Scanner Initialization: Scanner scanner = new Scanner(System.in); This

line initializes a Scanner object named scanner to read input from the console.

5. Array Declaration: double[] numbers = new double[20]; This line creates

an array named numbers capable of holding 20 double values.

6. Input Loop:

javaCopy code
for (int i = 0; i < 20; i++) { System.out.print("Enter number " + (i + 1) + ": "); numbers[i] =
scanner.nextDouble(); }
This loop iterates 20 times. It prompts the user to enter a number, reads the input

using scanner.nextDouble(), and stores each entered number into the

numbers array.

7. Product Calculation:

javaCopy code
double product = 1; for (double num : numbers) { product *= num; }
This loop calculates the product of the 20 numbers stored in the numbers array by

multiplying each number together and storing the result in the product variable.

8. Display Result:

javaCopy code
System.out.println("Product of the 20 numbers is: " + product);
This line prints the calculated product of the 20 numbers to the console.

9. Scanner Closure: scanner.close(); This line closes the Scanner object, freeing

up resources associated with it.

Overall, this program uses a loop to accept 20 numbers from the user, stores them in
an array, calculates their product, and then displays the product.

You might also like