36.5k views
2 votes
Create a Java program that asks the user to enter 10 integers into the console, then outputs the following to the console.

1.The sum of the integers entered.
2.The average of the integers entered.

User Tohster
by
7.4k points

1 Answer

2 votes

Answer:

Sure, here is a Java program that asks the user to enter 10 integers into the console, then outputs the sum and average of those integers:

```java

import java.util.Scanner;

public class IntegersProgram {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int sum = 0;

System.out.println("Enter 10 integers:");

for (int i = 0; i < 10; i++) {

int num = scanner.nextInt();

sum += num;

}

double average = (double) sum / 10;

System.out.println("Sum: " + sum);

System.out.println("Average: " + average);

}

}

```

This program first creates a `Scanner` object to read input from the console. It then prompts the user to enter 10 integers using a `for` loop that iterates 10 times. During each iteration, it reads an integer from the console using `scanner.nextInt()` and adds it to the `sum` variable.

After all 10 integers have been entered and added to the `sum`, the program calculates the average by dividing the `sum` by 10 and casting it to a `double`. Finally, it outputs both the `sum` and `average` to the console using `System.out.println()`.

User Roger Nelson
by
6.9k points