Saturday 4 February 2017

Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100

Code:

import java.util.*;
public class Score
{    
      public static void main(String[] args)
     {
          int a=0;
          double c = 0, sum=0;
          double[] b = new double[100];
          Scanner s = new Scanner(System.in);
          while (a < b.length)
         {
              System.out.println("Enter score");
              c = s.nextDouble();
              if (c >= 0)
             {
                  b[a] = c;
                sum =sum + b[a];
                a++;
             }
             else break;
        }
        double smaller = 0;
        double avg = sum/a;
        for (int i=0; i<a; i++)
        {
             if (b[i] < avg)
              smaller++;
         }
         System.out.println("The average is: " + avg);
         System.out.println("Scores  above or equal to the average are: " +(a-smaller) );
         System.out.println("Scores below average are: " +smaller);
      }
}

Output:


No comments:

Post a Comment