Wednesday, January 7, 2015

Java program to calculate the average of five numbers


The program given below is a Java program that will add five numbers, will calculate the average and later the result will be displayed. Five float type variables n1, n2, n3, n4 and n5 are used to store five numbers and another variable avg to store the result. This java program won't get any input from the keyboard. That's why the variables are initialized with some values.
java program,calculating average of five numbers

The following Java program will calculate the average of five numbers
public class Avg {
public static void main(String [] args) {
float n1=5;
float n2=7;
float n3=9;
float n4=1;
float n5=6;
float avg=0;

avg=(n1+n2+n3+n4+n5)/5;

System.out.println("The average is="+avg);
}
}


No comments:

Post a Comment