Java Program to Calculate Percentage of Secured Mark

In the previous article, we have seen Java Program to Calculate Basal Metabolic Rate

In this article we are going to see how to find percentage of secured marks using Java programming language.

Java Program to Calculate Percentage of Secured Mark

Percentage marks can be calculated by dividing the sum of the obtained marks by the total marks and multiplying the result by 100.

Let’s see different ways to find percentage of secured marks.

Method-1: Java Program to Calculate Percentage of Secured Mark By Using User Defined Method

Approach:

  1. Create Scanner class object.
  2. Ask the user to enter “total number of subjects”, “full marks per subject”.
  3. Create an array with size of total number of subjects.
  4. Ask the user to enter “his/her marks obtained per subject” and store them in the created array.
  5. Define a static method to find the sum of the array.
  6. In sum method, initialize a sum variable to 0
  7. Use a for loop from i=0 to arr.length. For each iteration, add arr[i] to the sum variable.
  8. Return the sum.
  9. Now call a user defined method findPercentage() to find the percentage of secured mark.
  10. Now calculate sum by dividing sum of the array(obtained from sum() method) with totalMarks x number of subjects and multiplying the result by 100.
  11. The values have been type casted to double because, otherwise it will truncate the decimal value while calculating the percentage.

 Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //ask user to total number of subjects
        System.out.print("Enter number of subjects: ");
        int num_subs = sc.nextInt();
        //inetger array 'marks' declared to hold the marks of subjects
        int[] marks = new int[num_subs];
        //ask user to enter full marks per subject
        System.out.print("Enter full marks per subject: ");
        int fullMarks = sc.nextInt();
        
        //ask user to enter marks for each subject
        System.out.println("Enter your marks: ");
        //using for loop taking input of marks for each subject
        for (int i = 0; i < num_subs; i++) 
        {
            marks[i] = sc.nextInt();
            // exit loop if user enters invalid marks
            if (marks[i] > fullMarks) 
            {
                System.out.println("Invalid marks");
                System.exit(0);
            }
        }
        
        //find total secured mark by calling sum() method 
        //and store in double variable 'total'
        //'marks' integer array passed as parameter
        double total=(double) sum(marks);
        
        //call findPercentage() method to find percentage
        //'total' mark, number of subjects and fullMarks are passed as parameter
        findPercentage(total, num_subs, fullMarks);
    }
    
    //user defined method findPercentage() to calculate percentage
    public static void findPercentage(double total, int num_subs, int fullMarks)
    {
        // calculate percentage
        double percentage = total / ((double) num_subs * fullMarks) * 100;
        // display percentage
        System.out.println("Your percentage is " + percentage + "%");
    }
    
    //user defined method sum() to find sum of total secured mark
    public static int sum(int[] arr) 
    {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) 
        {
            sum += arr[i];
        }
        return sum;

    }
}
Output:

Enter number of subjects: 6
Enter full marks per subject: 100
Enter your marks: 
86
92
78
89
72
85
Your percentage is 83.66666666666667%

Method-2: Java Program to Calculate Percentage of Secured Mark By Using User Input Value

Approach:

  1. Create Scanner class object.
  2. Ask the user to enter “total number of subjects”, “full marks per subject”.
  3. Create an array with size of total number of subjects.
  4. Ask the user to enter “his/her marks obtained per subject” and store them in the created array.
  5. Define a static method to find the sum of the array.
  6. In sum method, initialize a sum variable to 0
  7. Use a for loop from i=0 to arr.length. For each iteration, add arr[i] to the sum variable.
  8. Return the sum.
  9. Now calculate sum by dividing sum of the array(obtained from sum() method) with totalMarks x number of subjects and multiplying the result by 100.
  10. The values have been type casted to double because, otherwise it will truncate the decimal value while calculating the percentage.

 Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //ask user to total number of subjects
        System.out.print("Enter number of subjects: ");
        int num_subs = sc.nextInt();
        //inetger array 'marks' declared to hold the marks of subjects
        int[] marks = new int[num_subs];
        //ask user to enter full marks per subject
        System.out.print("Enter full marks per subject: ");
        int fullMarks = sc.nextInt();
        
        //ask user to enter marks for each subject
        System.out.println("Enter your marks: ");
        //using for loop taking input of marks for each subject
        for (int i = 0; i < num_subs; i++) 
        {
            marks[i] = sc.nextInt();
            // exit loop if user enters invalid marks
            if (marks[i] > fullMarks) 
            {
                System.out.println("Invalid marks");
                System.exit(0);
            }
        }
        
        // calculate percentage
        double percentage = (double) sum(marks) / ((double) num_subs * fullMarks) * 100;
        // display percentage
        System.out.println("Your percentage is " + percentage + "%");
    }
    
    //user defined method sum() to find sum of total secured mark
    public static int sum(int[] arr) 
    {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) 
        {
            sum += arr[i];
        }
        return sum;

    }
}
Output:

Enter number of subjects: 5
Enter full marks per subject: 100
Enter your marks: 
75
90
82
86
79
Your percentage is 82.39999999999999%

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: