Java Program of Pass and Fail Count

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:

Count.java

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");
        }
        
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

Your email address will not be published. Required fields are marked *