In this tutorial you will learn about the Java Program to Calculate CGPA and its application with practical example.
In this tutorial, we will learn to create a Java Program to Calculate CGPA using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
CGPA(Cumulative Grade Point Average)
Cumulative Grade Point Average (CGPA) is a tool used to evaluate your academic performance.
For conversion of CGPA to percentage, the CGPA must be multiplied with 9.5.
Example. if your score in Cgpa is 9.7 ,then in “%”, it will be 9.7×9.5=92.1%.
Java Program to Calculate CGPA
In this program we will learn to create a program that will Calculate CGPA and Percentage from CGPA. Firstly we declare required variable and also initiates required values in variable. Next we take value from user at run time and then after we will find CGPA and Percentage from CGPA .Lets have a look at the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// Java program to calculate the CGPA and CGPA percentage. import java.util.Scanner; class Calculate_CGPA { public static double Calculate_CGPA(double[] marks, int number) { //Declaring variables.. double grade[] = new double[number]; double cgpa, sum = 0; for (int i = 0; i < number; i++) { grade[i] = (marks[i] / 10); } for (int i = 0; i < number; i++) { sum += grade[i]; } cgpa = sum / number; return cgpa; } public static void main(String args[]) { int number = 5; double[] marks= { 90, 80, 80, 80, 90 }; double cgpa = Calculate_CGPA(marks, number); System.out.println("CGPA = " + cgpa); System.out.println("CGPA Percentage = "+ String.format("%.2f", cgpa * 9.5)); } } |
Output
In the above program, we have first declared and initialized a set of variables required in the program.
- number= it will hold size of number.
- marks= it will hold given marks.
- sum= it wil hold sum of values.
- cgpa= it will give value result in cgpa.
First of all we declared and initialized set of variables required in the program. Then we will take a value and pass this value to function Calculate_CGPA(marks,number).
With in the Function we will Calculate both the answer in CGPA and also in percentage.
And return the result back to main function where we print the value in CGPA and in Percentage.
To get value form CGPA just simply multiply with 9.5 as shown in above picture.