Java Program to find the average of 5 subjects


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


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

int avg = sum / 5;

Example: How to find the average of 5 subjects in java.

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("Sum of all subjects:" + sum);
        System.out.println("Average of subjects:" + avg);

    }
}

Output:

Enter five subject marks:10 20 30 40 50
Sum of all subjects:150.0
Average of subjects:30.0