Java Program to find average of 5 subjects


In this program, You will learn how to find average of 5 subjects in Java.


int sum = a + b + c + d + e;

int avg = sum / 5;


Example: How to find average of 5 subjects in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        float a, b, c, d, e, sum, avg;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Five Subject Marks :");

        a = sc.nextFloat();
        b = sc.nextFloat();
        c = sc.nextFloat();
        d = sc.nextFloat();
        e = sc.nextFloat();

        sum = a + b + c + d + e;
        avg = sum / 5;

        System.out.println("\nThe Sum of All Subjects is :" + sum);
        System.out.println("The Average of Subjects is :" + avg);

    }
}

Output:

Enter Five Subject Marks :2 3 4 5 6
The Sum of All Subjects is :20.0
The Average of Subjects is :4.0