wap a java enter the average of 20 even numbers .
Answers
Answer:
// Program to find average of even numbers
// till a given even number.
import java.io.*;
class GFG {
// Function to calculate the average
// of even numbers
static int averageEven(int n)
{
if (n % 2 != 0) {
System.out.println("Invalid Input");
return -1;
}
int sum = 0, count = 0;
while (n >= 2) {
// count even numbers
count++;
// store the sum of even numbers
sum += n;
n = n - 2;
}
return sum / count;
}
// driver function
public static void main(String args[])
{
int n = 16;
System.out.println(averageEven(n));
}
}
Answer:
wap a java enter the average of 20 even numbers .