Here we are explaining how to write a java program to find out the average of three numbers provided by the user. So first we have to read three integer numbers from the user and then we will find out the average of that numbers.
First, we have to declare the class AverageNumber.Then we have to declare the variables a,b,c, to hold the three integer numbers. Then read the numbers from the user using the scanner class object. Then find out the sum of the integer numbers and store them in the variable sum. Now calculate the average as avg=sum/3 and display the result using system.out.println()
.
STEP 1: Declare the class AverageNumber with a public modifier.
STEP 2: Open the main() to start the program, Java program execution starts with the main()
STEP 3: Declare the integer variables a,b,c,sum.
STEP 4: Create the object of the scanner class as sc.
STEP 5: Read the three numbers into the variables a,b,c.
STEP 6: Findout sum as sum=a+b+c.
STEP 7: Calculate average as avg=sum/3.
STEP 8: Display average as avg.
import java.util.Scanner;
public class AverageNumber
{
public static void main(String[] args)
{
int a,b,c,sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
a = sc.nextInt();
System.out.println("Enter the second number:");
b = sc.nextInt();
System.out.println("Enter the third number:");
c = sc.nextInt();
sum=a+b+c;
double avg=sum/3;
System.out.println("Average of the numbers is "+avg);
}
}
Enter the first number:10 Enter the second number:10 Enter the third number:10 Average of the numbers is 10.0