Java Program print 20 numbers
Java Program print 20 numbers
TWENTY NUMBERS
import java.util.Scanner;
System.out.println("Enter 20 numbers:");
double product = 1;
// Calculating product
for (double num : numbers) {
product *= num;
}
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.
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.
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
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
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.