draw a program to find average of three numbers
Answers
-->Using Variable Initialization:
public class Average
{
public static void main(double a, double b, double c)
{
double sum = a + b + c;
double avg = sum/3;
System.out.println ("Average: " + avg );
}
}
-->Using Scanner:
import java.util.Scanner;
public class average
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in); System.out.print("Enter the first number: ");
double num1 = scan.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scan.nextDouble();
System.out.print("Enter the third number: ");
double num3 = scan.nextDouble();
scan.close();
System.out.print("The average of entered numbers is:" + avr(num1, num2, num3));
}
public static double avr(double a, double b, double c)
{
return (a + b + c) / 3;
}
}
This program is in Java.
import java.util.*;
public class AverageCalculator {
public static void main (String [] args) {
//Taking input from the user
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter the second number: ");
double num2 = sc.nextDouble();
System.out.print("Enter the third number: ");
double num3 = sc.nextDouble();
//Main part comes here
double Average = (num1 + num2 + num3) / 3;
System.out.println("The average of the given numbers is " + Average);
}
}
Output:
Enter the first number: 30
Enter the second number: 31
Enter the third number: 28
The average of the given numbers is 29.666666666666668