Java Program to calculate Average of three numbers- 6 ways

Java Program to calculate Average of three numbers- 6 ways

The java program to calculate average of three numbers has been written in five different ways such as Scanner class, direct value initialization, Buffered Reader class, Command Line argument, methods, and using Constructor. Java Program to calculate Average of three numbers- 6 ways are given below:

1. Java program to calculate Average of three numbers using Scanner class:

Program:

import java.io.*;
import java.util.*;
class AverageOfThreeNo
{
public static void main(String args[])
{
int a,b,c,avg;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
System.out.println("Enter the value of c");
c=sc.nextInt();
System.out.println("The average of three no. is :\n");
int sum=a+b+c; 
avg=sum/3;
System.out.println(avg);
}
}

Output:

Enter the value of a
10
Enter the value of b
5
Enter the value of c
6
The average of three n0. is : 7

2. Java program to calculate Average of three numbers using Method:

Program:

import java.io.*;
import java.util.*;
class CrArea
{
public static void main(String args[])
{
float a,b,c,avg;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextFloat();
System.out.println("Enter the value of b");
b=sc.nextFloat();
System.out.println("Enter the value of c");
c=sc.nextFloat();
System.out.println("The average of three no. is :"+average(a,b,c));
}
public static float average(float a, float b, float c)
{
return (a+b+c)/3;
}
}

Output:

Enter the value of a
10
Enter the value of b
5
Enter the value of c
6
The average of three no. is : 7.0

3. Java program to calculate Average of three numbers using Constructor:

Program:

import java.util.*;
class Average
{
	double avg;
	Average(double a, double b, double c)
	{
	 avg= (a+b+c)/3;
 
	}
}
class AverageNumber
{
   public static void main(String args[]) 
    {   
      Scanner sc= new Scanner(System.in);
System.out.println("Enter the value of a1");
      double a1= sc.nextDouble();   
System.out.println("Enter the value of a2");
      double a2= sc.nextDouble();      
System.out.println("Enter the value of a3");
      double a3= sc.nextDouble();      	  
      Average obj =new Average(a1, a2, a3);
	  System.out.println("Average of three number is :" + obj.avg);      
   }
 }

Output:

Enter the value of a1
10
Enter the value of a2
5
Enter the value of a3
6
Average of three no. is: 7.0

4. Java program to calculate Average of three numbers using Command Line Argument:

Program:

import java.util.*;
class AverageNumber
{
public static void main(String args[])
{
double avg;
int a1= Integer.parseInt(args[0]);
System.out.println(a1);
int a2= Integer.parseInt(args[1]);
System.out.println(a2);
int a3= Integer.parseInt(args[2]);
System.out.println(a3);
avg=(a1+a2+a3)/3;
System.out.println("The average of three number is "+avg);
}
}

Compile:

javac AverageNumber.java

Run:

java AverageNumber 10 5 6
The average of three number is 7.0

5. Java program to calculate Average of three numbers using Buffered Reader Class:

Program:

import java.util.*;
import java.lang.*;
import java.io.*;
class CrArea1
{
public static void main(String args[]) throws Exception
{
double avg;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of a");
int a= Integer.parseInt(br.readLine());
System.out.println("Enter the value of b");
int b= Integer.parseInt(br.readLine());
System.out.println("Enter the value of c");
int c= Integer.parseInt(br.readLine());
avg=(a+b+c)/3;
System.out.println("The average of three number is "+avg);
}
}

Output:

Enter the value of a
10
Enter the value of b
2
Enter the value of c
6
The average of three number is 6.0

6. Java program to calculate Average of three numbers using Direct value Initialization:

Program:

import java.util.*;
import java.lang.*;
import java.io.*;
class AverageNumber
{
public static void main(String args[]) throws Exception
{
double avg;
double a=5.0, b=6.0, c=7.0;
avg=(a+b+c)/3;
System.out.println("The average of three number is "+avg);
}
}

Output:

The average of three number is 6.0

Recommended Post:

1. Java program to calculate multiplication of two numbers without using * operator?

2. Java program to calculate Area and Circumference of circle Using 5 Different ways?

3. Difference between Abstract class and Interface?

4. Difference Class and Object?

5. Java Program to calculate multiplication of two numbers using 5 different ways?

6. Java program to calculate Area of triangle and Perimeter of triangle using 5 different ways?

Leave a Comment