Enter the number of subjects and then enter marks if students in all those subjects. Now we us for loop to calculate total marks of the student and hence divide it by the total number of subjects to get percentage marks.
Here is the source code of the Java Program to Accept the Marks of a Student into a 1D Array and find Total Marks and Percentage. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Student_Marks
public static void main(String args)
int n, total = 0, percentage;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of subject:");
n = s.nextInt();
int marks = new int[n];
System.out.println("Enter marks out of 100:");
for(int i = 0; i < n; i++)
marks[i] = s.nextInt();
total = total + marks[i];
percentage = total / n;
$ javac Student_Marks.java $ java Student_Marks Enter no. of subject:5 Enter marks out of 100: 86 89 91 82 78 Sum:426 Percentage:85
Sanfoundry Global Education & Learning Series – 1000 Java Programs.