How to write a java program to calculate CGPA percentage. Java code to calculate the CGPA ( Cumulative Grade Point Average ), in 5 different ways. Using standard values, using command line arguments, inputs through scanner class and user-defined method. Cumulative Grade Point Average ( CGPA ) is the overall grade point average. Definition and sample programs are described below about GPA. Check more programs for beginners and interview question and answers here.
[table id=2 /]
- What is CGPA?
A: CGPA is overall grade point average. For each course, there are certain credits which are usually based on how many hours the class is held in each week.
- How to calculate the CGPA and formula?
A: Your grade point average (GPA) is calculated by dividing the total amount of grade points earned by the total amount of credit hours attempted. Your grade point average may range from 0.0 to a 4.0. To get the example student’s GPA, the total grade points are divided by the total credit hours attempted. Here is the simple formula to calculate the GPA ( Grade point average formula )
Calculate GPA – Java Program In Five Ways
Program To Find CGPA – 1 ( Using standard values with outputs )
[wp_ad_camp_3]
Here we wrote the code with standard values. We also embedded an online execution tool below, do check it out. Standard values are nothing but using the above formula to find the GPA in the below program. For, every code we added three to four different types of outputs so that you will get an idea.
Java Code using standard values here.
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 32 33 34 35 36 37 38 39 40 |
class CGPA { public static void main(String arg[]) { int n=5; double m[]=new double[n]; double g[]=new double[n]; double cgpa,sum=0; m[0]=95; m[1]=85; m[2]=75; m[3]=80; m[4]=95; for(int i=0;i<n;i++) { g[i]=(m[i]/10); } for(int i=0;i<n;i++) { sum+=g[i]; } cgpa=sum/n; System.out.println("cgpa="+cgpa); System.out.println("percantage from cgpa="+cgpa*9.5); } } |
Output:
1 2 |
cgpa=8.6 percantage from cgpa=81.7 |
Sample Program GPA – 2 ( Using Command-Line Arguments )
Here we go another program using command line arguments. If you have no idea about command line arguments check out here: the Complete Guide. In simple command lines are nothing but giving inputs on the console it itself. Check out the code and output.
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 32 33 34 35 36 37 |
class CGPA { public static void main(String args[]) { int n; n=args.length; double marks[]=new double[n]; double grade[]=new double[n]; double cgpa,sum=0; for(int i=0;i<n;i++) { marks[i]=Long.parseLong(args[i]); } for(int i=0;i<n;i++) { grade[i]=(marks[i]/10) ; } for(int i=0;i<n;i++) { sum+=grade[i]; } cgpa=sum/n; System.out.println("cgpa="+cgpa); System.out.println("percantage from cgpa="+cgpa*9.5); } } |
Output ( Using command line arguments ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
output:1 javac CGPA.java java CGPA 85 95 66 77 25 cgpa=6.960000000000001 percantage from cgpa=66.12 output:2 javac CGPA.java java CGPA 90 95 98 99 100 cgpa=9.64 percantage from cgpa=91.58000000000001 output:3 javac CGPA.java java CGPA 85 95 66 77 55 cgpa=7.5600000000000005 percantage from cgpa=71.82000000000001 output:4 javac CGPA.java java CGPA 70 70 70 70 70 cgpa=7.0 percantage from cgpa=66.5 |
Calculate GPA Using Arrays
[wp_ad_camp_3]
Here we go another method to calculate a grade point average, taking inputs through scanner class method.
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 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.Scanner; class CGPA { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter number of subjects"); int n=sc.nextInt(); double[] marks=new double[n]; System.out.println("Enter marks"); for(int i=0;i<n;i++) { marks[i]=sc.nextInt(); } double grade[]=new double[n]; double cgpa,sum=0; for(int i=0;i<n;i++) { grade[i]=(marks[i]/10) ; } for(int i=0;i<n;i++) { sum+=grade[i]; } cgpa=sum/n; System.out.println("cgpa="+cgpa); System.out.println("percantage from cgpa="+cgpa*9.5); } } |
Output ( Inputs Through Scanner Class ):
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 32 33 34 35 36 |
output:1 Enter number of subjects 5 Enter marks 90 80 70 80 90 cgpa=8.2 percantage from cgpa=77.89999999999999 output:2 Enter number of subjects 6 Enter marks 90 80 78 87 50 45 cgpa=7.166666666666667 percantage from cgpa=68.08333333333334 output:3 Enter number of subjects 5 Enter marks 75 85 88 98 65 cgpa=8.22 percantage from cgpa=78.09 |
Java Program By Using Method
Java code for calculating CGPA using a user-defined method with sample outputs and execution tool. Check it out!
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import java.util.Scanner; class CGPA { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter number of subjects"); int n=sc.nextInt(); double[] marks=new double[n]; System.out.println("Enter marks"); for(int i=0;i<n;i++) { marks[i]=sc.nextInt(); } double cgpa,sum; sum= cgpaCalculation(marks); cgpa=sum/n; System.out.println("cgpa="+cgpa); System.out.println("percantage from cgpa="+cgpa*9.5); } static double cgpaCalculation(double marks[]) { double sum=0; double grade[]=new double[marks.length]; for(int i=0;i<marks.length;i++) { grade[i]=(marks[i]/10) ; } for(int i=0;i<marks.length;i++) { sum+=grade[i]; } return sum; } } |
Output ( User define method ) :
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 32 33 34 35 36 37 |
output:1 Enter number of subjects 5 Enter marks 100 98 78 48 59 cgpa=7.659999999999999 percantage from cgpa=72.77 output:2 Enter number of subjects 6 Enter marks 90 90 90 90 90 90 cgpa=9.0 percantage from cgpa=85.5 output:3 Enter number of subjects 5 Enter marks 100 95 90 80 85 cgpa=9.0 percantage from cgpa=85.5 |
Sample Program – 5 (using class)
Here we go another version of a program to calculate the GPA by creating a new separate class for ( CGPAcalculation) and taking inputs using scanner class method with sample outputs.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import java.util.Scanner; class CGPACalculation { double sum=0; CGPACalculation(double marks[]) { double grade[]=new double[marks.length]; for(int i=0;i<marks.length;i++) { grade[i]=(marks[i]/10) ; } for(int i=0;i<marks.length;i++) { sum+=grade[i]; } } } class CGPA { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter number of subjects"); int n=sc.nextInt(); double[] marks=new double[n]; System.out.println("Enter marks"); for(int i=0;i<n;i++) { marks[i]=sc.nextInt(); } double grade[]=new double[n]; double cgpa,sum; CGPACalculation s= new CGPACalculation(marks); cgpa=s.sum/n; System.out.println("cgpa="+cgpa); System.out.println("percantage from cgpa="+cgpa*9.5); } } |
Output:
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 |
output:1 Enter number of subjects 1 Enter marks 100 cgpa=10.0 percantage from cgpa=95.0 output:2 Enter number of subjects 4 Enter marks 100 90 80 60 cgpa=8.25 percantage from cgpa=78.375 output:3 Enter number of subjects 5 Enter marks 50 60 70 80 90 cgpa=7.0 percantage from cgpa=66.5 |
More Tutorials :