Pass and Fail Count Program in java

Question:

Ram has passed in certain subjects and failed in a few. Write a program to count the no of subjects he passed in and the no of subjects he has failed in. Marks scored below 50 is considered as failed. If Ram has passed in all the subject’s print “Ram passed in all subjects” and if failed print “Ram failed in all subjects”.

Assume maximum size of array is 20,

Sample Input 1:
Enter the no of subjects:
6
60
70
80
90
45
49

Sample Output 1:

Ram passed in 4 subjects and failed in 2 subjects

Sample Input 2:
Enter the no of subjects:

0

Sample Output 2:

Invalid input range

Sample Input 3:
Enter the no of subjects:

-2

Sample Output 3:

Invalid input range

CODE:

import java.util.*;
public class Count {
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.println("Enter the no of subjects:");
        int no_sub=sc.nextInt();
        
        if(no_sub>0 && no_sub<=20)
        {
            int marks[]=new int[no_sub];
            
            for(int i=0;i<no_sub;i++)
            {
                marks[i]=sc.nextInt();
            }

            int pass=0,fail=0;
            for(int i=0;i<no_sub;i++)
            {
                if(marks[i]<50)
                {
                    fail++;
                }
                else
                {
                    pass++;
                }
            }
            
                if(fail==0)
                {
                    System.out.println("Ram passed in all subjects");
                }
                else if(pass==0)
                {
                    System.out.println("Ram failed in all subjects");
                }
                else
                {
                    System.out.println("Ram passed in "+pass+" subjects and failed in "+fail+" subjects");
                }
        }
        else
        {
            System.out.println("Invalid input range");
        }
        
    }
}
Next:
  1. Search a Course
  2. Average and Grade Calculation
  3. String – Find and replace the character (first occurrence)
  4. Sort the first and second half of an array

Leave a Comment

Scroll to Top