Dremendo Tag Line

Input subject marks and check pass or fail in Java

if else if - Question 9

In this question, we will see how to input marks of English, Math and Computer of a student and check whether he is pass in the examination or not in Java programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.

Q9) Write a program in Java to input marks of English, Math and Computer of a student and check whether he is pass in the examination or not. For passing in an examination one must get above 40 in at least two subjects or the average of the three subjects must be greater than 40.

Program

import java.util.Scanner;

public class Q9
{
    public static void main(String args[])
    {
        int s1,s2,s3;
        float avg;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter marks obtained in 3 subjects");
        s1=sc.nextInt();
        s2=sc.nextInt();
        s3=sc.nextInt();
        avg=(s1+s2+s3)/3.0f;
        if(s1>40 && s2>40)
        {
            System.out.print("Pass");
        }
        else if(s2>40 && s3>40)
        {
            System.out.print("Pass");
        }
        else if(s3>40 && s1>40)
        {
            System.out.print("Pass");
        }
        else if(avg>40)
        {
            System.out.print("Pass");
        }
        else
        {
            System.out.print("Fail");
        }
    }
}

Output

Enter marks obtained in 3 subjects
56
34
45
Pass
video-poster