JAVA PROGRAM THAT WILL PRINT THE PRODUCT OF
TWENTY NUMBERS
import [Link];
public class TwentyNum {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
double[] numbers = new double[20];
[Link]("Enter 20 numbers:");
// Taking input for 20 numbers
for (int i = 0; i < 20; i++) {
[Link]("Enter number " + (i + 1) + ":");
numbers[i] = [Link]();
}
double product = 1;
// Calculating product
for (double num : numbers) {
product *= num;
}
// Displaying the product
[Link]("Product of the 20 numbers is: " +
product);
[Link]();
}
}
THE ABOVE CODES MEAN THE FOLLOWING
1. Imports: import [Link]; This line imports the Scanner class from
the [Link] 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([Link]); 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++) { [Link]("Enter number " + (i + 1) + ": "); numbers[i] =
[Link](); }
This loop iterates 20 times. It prompts the user to enter a number, reads the input
using [Link](), 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
[Link]("Product of the 20 numbers is: " + product);
This line prints the calculated product of the 20 numbers to the console.
9. Scanner Closure: [Link](); 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.