Java program to calculate total average and percentage of five subjects













Basic Java programming exercises

Java Tutorial Java Exercises Data Structures


Write a Java program to input marks of five subjects of a student and calculate total, average and percentage of all subjects. How to calculate total, average and percentage in Java programming. Logic to find total, average and percentage in Java program.








Required knowledge

Arithmetic operators, Variables and expressions, Data types, Basic input/output


Logic to find total, average and percentage

Step by step descriptive logic to find total, average and percentage.

  1. Input marks of five subjects. Store it in some variables say eng, phy, chem, math and comp.

  2. Calculate sum of all subjects and store in total = eng + phy + chem + math + comp.

  3. Divide sum of all subjects by total number of subject to find average i.e. average = total / 5.

  4. Calculate percentage using percentage = (average / 500) * 100.

  5. Finally, print resultant values total, average and percentage.


Program to find total, average and percentage
/** * Java program to calculate total, average and percentage of five subjects */ import java.util.Scanner; class Test { public static void main(String args[]) { float eng, phy, chem, math, comp; double total, average, percentage; Scanner op=new Scanner(System.in); /* Input marks of all five subjects */ System.out.println("Enter marks of five subjects:"); System.out.print("Enter marks of English subjects:"); eng=op.nextFloat(); System.out.print("Enter marks of physics subjects:"); phy=op.nextFloat(); System.out.print("Enter marks of chemistry subjects:"); chem=op.nextFloat(); System.out.print("Enter marks of maths subjects:"); math=op.nextFloat(); System.out.print("Enter marks of computers subjects:"); comp=op.nextFloat(); /* Calculate total, average and percentage */ total = eng + phy + chem + math + comp; average = (total / 5.0); percentage = (total / 500.0) * 100; /* Print all results */ System.out.println("Total marks ="+total); System.out.println("Average marks = "+average); System.out.println("Percentage = "+percentage); } }




Output:

Enter marks of five subjects.. Enter marks of English subjects:50 Enter marks of physics subjects:70 Enter marks of chemistry subjects:90 Enter marks of maths subjects:59 Enter marks of computers subjects:75 Total marks =344.0 Average marks = 68.8 Percentage = 68.8