Calculate average and check good or bad in Java
if else - Question 6
In this question, we will see how to input 5 decimal numbers and print good if their average is greater than 50, otherwise print bad in Java programming using the if else statement. To know more about if else statement click on the if else statement lesson.
Q6) Write a program in Java to input 5 decimal numbers and find their average. If the average is greater than 50 then print Good, otherwise print Bad.
Program
import java.util.Scanner;
public class Q6
{
public static void main(String args[])
{
float a,b,c,d,e,avg;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 decimal numbers");
a=sc.nextFloat();
b=sc.nextFloat();
c=sc.nextFloat();
d=sc.nextFloat();
e=sc.nextFloat();
avg=(a+b+c+d+e)/5;
System.out.println("Average=" + avg);
if(avg>50)
{
System.out.print("Good");
}
else
{
System.out.print("Bad");
}
}
}
Output
Enter 5 decimal numbers 86.23 75.18 92.10 69.35 78.14 Average=80.2 Good