Java Program to find Average of Three Numbers

In this tutorial we will write a program to find average of three numbersĀ . Here , we will discuss algorithm to find the average of three numbers then find average of three numbers in java .

Algorithm to find the average of three numbers

firstly , we will write an algorithm to find the average of three numbersĀ as below .

  1. Take input from user and store values in three variable num1 , num2 and num3 .
  2. Calculate the sum of these three numbers and assign them to the sum variable .
  3. Find average of given three numbers as avg = sum/3
  4. Print the value of the average variable(avg) .

Now we will implement above algorithms and write java program to find average of 3 numbers .

Example 1 : Java Program to find Average of Three Numbers

We will calculate average of three numbers using the + and / arithmetic operator . In this exmple we take input from user using scanner class and store double type variable . We calculate find sum of three number using + operator then find average of those number, divided sum by 3 .

import java.util.Scanner;

public class AverageOfThreeNumbers {
        public static void main(String[] args) {
			
        	//create scanner object 
        	Scanner scanner = new Scanner(System.in);
        	//take input from user 
            System.out.print("Enter the first number: ");
            double num1 = scanner.nextDouble();
            System.out.print("Enter the second number: ");
            double num2 = scanner.nextDouble();
            System.out.print("Enter the third number: ");
            double num3 = scanner.nextDouble();
            
            //calculate sum of given three numbers
            double sum = num1+num2+num3;
            
            //calculate average of given three number 
            double avg = sum / 3;
            
            //print the average of three number 
            System.out.println("Average of given three numbers is :"+ avg);
            
            //close scanner 
              scanner.close();
         }
}

Output :

Enter the first number: 100
Enter the second number: 70
Enter the third number: 80
Average of given three numbers is :83.33333333333333

Example 2: Java Program to find Average of Three Numbers Using Method

In this example we will write a program to find average of 3 numbers using method . We write here one method , which takes three double type parameter as input and return average of given numbers .Now let’s do code for average program in java .

import java.util.Scanner;

public class AverageOfThreeNumbers2 {
    public static void main(String[] args) {
		
    	//create scanner object 
    	Scanner scanner = new Scanner(System.in);
    	//take input from user 
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();
        System.out.print("Enter the third number: ");
        double num3 = scanner.nextDouble();
        
        //call average method 
        double avg = average(num1, num2, num3);
        
        //print the average of three number 
        System.out.println("Average of given three numbers is :"+ avg);
        
        //close scanner 
    	scanner.close();

	}
    
    //average method take three numbers input and return average of given numbers 
    public static double average(double num1 , double num2 , double num3){
    	return (num1+num2+num3)/3;
    }
    
    
}

Output :

Enter the first number: 100
Enter the second number: 80
Enter the third number: 90
Average of given three numbers is :90.0

In this tutorial , we learned average program in java for three numbers . Here , Firstly we written an algorithm to find the average of three numbers and then implement it and write program to find average of 3 numbers in java . You can see more java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 47 = 50