Java program will create an array to input five positive integer numbers. The pr
ID: 3812983 • Letter: J
Question
Java program will create an array to input five positive integer numbers. The program asks a user to input five integer numbers and store it in an array. Then, it calculates the average of numbers in an array.
1. Average should allow only two decimal places.
2. The program should allow user input via keyboard.
3. The program must use for loop to insert positive integer number to the array.
4. The program must use for loop to calculate sum of array values before calculating average
Please include comments
Explanation / Answer
program :
import java.util.Scanner;
public class Avg {
public static void main(String[] args)
{
int sum = 0;
float average;
Scanner s = new Scanner(System.in);
int a[] = new int[5];
System.out.println("Enter the 5 elements:");
for(int i = 0; i < 5 ; i++)
{
a[i] = s.nextInt();
}
for(int i = 0; i < 5 ; i++)
{
sum = sum + a[i];
}
System.out.println("Sum:"+sum);
average = (float)sum / 5;
System.out.printf("Average: = %.2f ", average);
}
}
output:
Enter the 5 elements:
789
456
250
10
8
Sum:1513
Average: = 302.60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.