1
public static void main(String[] args) {

    int avg=0,sum=0,percentage=0;

    Scanner input = new Scanner(System.in);

    int[]arr=new int [6];

    System.out.println("Enter all the elements: ");

    for(int i=0; i<6; i++)
    {
        arr[i]=input.nextInt();
        sum+=arr[i];
        avg=sum/6;
    }
    System.out.println("Average is: "+avg);

    for(int i=0;i<arr.length;i++)
    {
         if(arr[i]>avg)
        {
                percentage=(arr[i]*100/6);
        }
    }
    System.out.println("Percentage is: "+percentage+"%");

}

For example, if 3 of the elements of the array are greater than average, percentage is 3*100/6=50%

azro
  • 53,056
  • 7
  • 34
  • 70

6 Answers6

0

Your calculations are wrong at the bottom part.

for(int i=0;i<arr.length;i++){
  if(arr[i]>avg)
    {
       percentage=(arr[i]*100/6);
    }
}

here, the correct calculation would be

for(int i=0;i<arr.length;i++){
  if(arr[i]>avg)
    {
       percentage += (100/6);
    }
}

you have to add the count the number of times arr[i] is greater than average and multiply the count with 100/6. The above code does exactly that.

NB: The percentage variable should be a floating point number not an integer

s4k1b
  • 2,955
  • 1
  • 7
  • 14
0

You need to find the total number of values that are greater than the average. Then you take that total number and divide it by the max, then multiple by 100 to return the percentage. For example, if we use 6, 5, 4, 3, 2, 1; The average is 3. The total number greater than 3 is 3 (6, 5, 4). We then take 3 (total) and divide it by max (6) to get .5 then multiply by 100 to get 50 (50%).

array = 6, 5, 4, 3, 2, 1
average = 3
max = 6
percentage = average / max * 100
    public static void main(String[] args) {

        int avg=0,sum=0,percentage=0;

        Scanner input = new Scanner(System.in);

        int[]arr=new int [6];

        System.out.println("Enter all the elements: ");

        for(int i=0; i<6; i++)
        {
            arr[i]=input.nextInt();
            sum+=arr[i];
            avg=sum/6;
        }
        System.out.println("Average is: "+avg);

        int greaterThan = 0;

        for(int i=0;i<arr.length;i++)
        {
             if(arr[i]>avg) {
                    greaterThan++;
            }
        }
        percentage = (int) (((double) greaterThan / (double) arr.length) * 100D);

        System.out.println("Percentage is: "+percentage+"%");

 }
Jason
  • 5,154
  • 2
  • 12
  • 22
0

First, to compute the global average, do it at the end, using the array length tobe more generic

double sum=0;
for(int i=0; i<6; i++){
    arr[i] = input.nextInt();
    sum += arr[i];
}
avg = sum/input.length;
System.out.println("Average is: "+avg);

Then to compute the part that are the greater than the average, find how many values are greater then divide by the total number :

double greaterAvg = 0;
for(int i=0;i<arr.length;i++){
    if(arr[i]>avg){
            greaterAvg++;
    }
}
double percentage = 100 * greaterAvg / input.length
System.out.println("Percentage is: "+percentage+"%");

I used double type to remove int division problem : Int division: Why is the result of 1/3 == 0?

azro
  • 53,056
  • 7
  • 34
  • 70
0

You can use a counter variable initialized to zero to count the elements first, then you can simply count the percentage using COUNTER * 100 / (number of elements)

Tom
  • 2,734
  • 2
  • 22
  • 39
0

To calculate the percentage of elements having a value greater than average. first, you have to find the count of those elements and then divide it with the total number of elements

Try this:

public static void main(String[] args) {

    int avg=0,sum=0,percentage=0;

    Scanner input = new Scanner(System.in);

    int[]arr=new int [6];

    System.out.println("Enter all the elements: ");

    for(int i=0; i<6; i++)
    {
        arr[i]=input.nextInt();
        sum+=arr[i];
        avg=sum/6;
    }
    System.out.println("Average is: "+avg);

    int count = 0;
    for(int i=0;i<arr.length;i++)
    {
         if(arr[i]>avg)
        {
          count=count+1;
        }
    }
    System.out.println(count);
    percentage = (count*100)/6;
    System.out.println("Percentage is: "+percentage+"%");

 }

Output:

Enter all the elements:
1 2 3 4 5 6
Average is: 3
3
Percentage is: 50%
GOVIND DIXIT
  • 1,748
  • 10
  • 27
0

You're calculating the avg in the loop, that's not efficient as the value get crushed at each iteration. It's better to keep only the sum in the loop and calculated the avg after the first loop.

This being said, there are only two main (functional) issues in your code:

  • The first one is the percentage processing in the loop - you're missing the "+=" and should not use arr[i] value.

  • The second one is the accuracy of the percentage, should be double (e.g) instead of int (same for the average - if really needed).

To sum up :

  • Declare percentage as double : double percentage = 0;
  • Replace percentage=(arr[i]*100/6); => percentage += (100.0/6);
  • [Optional] Truncate/round the percentage for display: e.g. System.out.printf("Percentage is: (%.2f) %%", percentage);

Cheers!

Abs
  • 300
  • 1
  • 10