Header Ads

Write a program in Java to input name, roll number, class, marks of a student in English, Maths, Physics, Chemistry and Computer. Print total marks and grade

 Print total marks and grade on the basis of :-

  1. if percentage < 33 grade is F
  2. if percentage >= 33 and percentage < 45 grade is D
  3. if percentage >= 45 and percentage < 60 grade is C
  4. if percentage >= 60 and percentage < 75 grade is B
  5. if percentage >=75 grade is A

Approach :

  1. import scanner class 
  2. take necessary inputs from the user
  3. calculate total and percentage
  4. print desired output
  5. assign and print grades

Code :

package com.StepTowardsCoding;
import java.util.Scanner;

public class grade {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input from the user
System.out.print("Enter Name : ");
String n = sc.nextLine();
System.out.print("Enter Class : ");
String c = sc.nextLine();
System.out.print("Enter Roll No. : ");
int r = sc.nextInt();

System.out.print("Enter Marks in English : ");
float eng = sc.nextFloat();
System.out.print("Enter Marks in Maths : ");
float mat = sc.nextFloat();
System.out.print("Enter Marks in Physics : ");
float phy = sc.nextFloat();
System.out.print("Enter Marks in Chemistry : ");
float chem = sc.nextFloat();
System.out.print("Enter Marks in Computer : ");
float comp = sc.nextFloat();

// calculating total and percentage
float total = eng + mat + phy + chem + comp;
float per = total / 5;

// printing
System.out.println();
System.out.println("Name : " + n);
System.out.println("Roll No. : "+r);
System.out.println("Class : " + c + "\n");
System.out.println("Total Marks : " + total);
System.out.println("Percentage : " + per);

// assigning and printing grades
if (per >= 75) {
String g = "A";
System.out.println("Grade : " + g);
} else if (per >= 60) {
String g = "B";
System.out.println("Grade : " + g);
} else if (per >= 45) {
String g = "C";
System.out.println("Grade : " + g);
} else if (per >= 33) {
String g = "D";
System.out.println("Grade : " + g);
} else {
String g = "F";
System.out.println("Grade : " + g);
}
}
}

Output :

Enter Name : Abhay Kumar
Enter Class : 10
Enter Roll No. : 1
Enter Marks in English : 80
Enter Marks in Maths : 80
Enter Marks in Physics : 80
Enter Marks in Chemistry : 80
Enter Marks in Computer : 80

Name     : Abhay Kumar
Roll No. : 1
Class    : 10

Total Marks : 400.0
Percentage  : 80.0
Grade       : A

Explanation :

Take a inputs from the user name and class in string type, rollno. in integer type and marks in float or double type.
Note : Don't use sc.next(); to input strings, instead use sc.nextLine();
       sc.next(); will take the first string input normally, but when you press enter second string will take enter as input and the program will proceed without taking your input. This problem is solved by using sc.nextLine(): while taking multiple string inputs.
After taking all inputs, calculate total by adding all marks and percentage by dividing total by 5.
Print name, class, rollno., total marks and percentage.
'\n' is a special character used to leave a line 
Now create if-else statements to assign grades according to the percentage.

Post a Comment

0 Comments