In this tutorial you will learn about the Java Program to Find average of 3 numbers and its application with practical example.
In this tutorial, we will learn to create a Java Program to Find average of 3 numbers using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is average of numbers.
Average of numbers is by adding up all the numbers and dividing by size of numbers are in the set.
Java Program to Find average of 3 numbers
In this program we would find average of given number.first of we would take a values from user and find the average of numbers.let have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// java program to find average of three numbers.. import java.util.Scanner; public class Average { public static void main(String[] args) { // Decalring varaiables and taking values from user.. Scanner scan = new Scanner(System.in); System.out.print("Enter the first number: "); double number = scan.nextDouble(); System.out.print("Enter the second number: "); double number1 = scan.nextDouble(); System.out.print("Enter the third number: "); double number2 = scan.nextDouble(); scan.close(); // Passing value to fun avg().. System.out.print("The average of entered numbers is:" + avg(number, number1, number2) ); } // Calculating average of numvbers... public static double avg(double n1, double n2, double n3) { return (n1 + n2 + n3) / 3; } } |
Output
Average of Three Numbers.
In the above program, we have first declared and initialized a set variables required in the program.
- number,number1 and number2 = it will hold entered numbers.
After that we take a numbers from user and find average of numbers.
after getting values from users we pass the number to function avg().
In this Function we take three parameter as a arguments and add all three values and divide the sum by 3 to get the desire output and return the average to main function and where we will print the result of program.
Above image is the output of above program.