Wednesday, April 20, 2016

Write a code in which keep taking marks from user until user enter a negative number and calculate grade and at end give total number of A,B,C,D,E and F grades.

In this question we are required to  create a program which we will keep taking marks from user until user enter a negative number and then calculate grade after every input .At end give total number of A,B,C,D,E and F grades.
So here is the JAVA code for solving that problem.




package labsessional2;
import java.util.Scanner;
public class LabSessional2
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
   
        int a,GA=0,GB=0,GC=0,GD=0,GE=0,GF=0;
        while(true)
        {
            System.out.print("Enter marks: ");
            a=input.nextInt();
            if(a<0)
                break;
       
         String R= checkGrade(a);
           if(R=="A")
               GA++;
           else if(R=="B")
               GB++;
           else if(R=="C")
               GC++;
           else if(R=="D")
               GD++;
           else if(R=="E")
               GE++;
           else if(R=="F")
               GF++;
           
       
        }
        System.out.println("A grades are: "+GA);
        System.out.println("B grades are: "+GB);
        System.out.println("C grades are: "+GC);
        System.out.println("D grades are: "+GD);
        System.out.println("E grades are: "+GE);
        System.out.println("F grades are: "+GF);
    }
    static String checkGrade(int x)
    {
        String R="";
        if(x>=0 && x<=50)
            R="F";
        else if(x>50 && x<=60)
            R="E";
        else if(x>60 && x<=70)
            R="D";
        else if(x>70 && x<=80)
            R="C";
        else if(x>80 && x<=90)
            R="B";
        else if(x>90 && x<=100)
            R="A";
        return R;
       
    }

}

If you found any issue with the code then you can ask freely.
Keep visiting and keep learning.

0 comments:

Post a Comment