Exercise
Average
Objetive
Write a java program to calculate and display the average of four numbers entered by the user.
Example Code
// Importing the Scanner class to handle input from the user
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Declare a scanner to read user input
Scanner sc = new Scanner(System.in);
// Declare variables for the four numbers
System.out.print("Enter the first number: ");
double num1 = sc.nextDouble(); // Read the first number
System.out.print("Enter the second number: ");
double num2 = sc.nextDouble(); // Read the second number
System.out.print("Enter the third number: ");
double num3 = sc.nextDouble(); // Read the third number
System.out.print("Enter the fourth number: ");
double num4 = sc.nextDouble(); // Read the fourth number
// Calculate the average
double average = (num1 + num2 + num3 + num4) / 4;
// Display the result
System.out.println("The average is: " + average);
}
}