Print total marks and grade on the basis of :-
- if percentage < 33 grade is F
- if percentage >= 33 and percentage < 45 grade is D
- if percentage >= 45 and percentage < 60 grade is C
- if percentage >= 60 and percentage < 75 grade is B
- if percentage >=75 grade is A
Approach :
- import scanner class
- take necessary inputs from the user
- calculate total and percentage
- print desired output
- 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.
0 Comments
Ask Your Queries in the comments