In: Computer Science
Java Program Problem
// Description: Read five positive integers and average
them.
// Enter a negative integer to end the program.
import java.util.Scanner;
public class Lab5
{
public static void main (String args[])
{
Scanner sc;
sc = new Scanner(System.in);
final int MAX_NUMS = 5;
int num;
// variable to store the number
int sum; // variable to
store the sum
int count; // variable
to store the total numbers read
int close; // loop
control variable
close = 1; // initialize loop control variable
while (<condition goes
here>)
{
System.out.println("Calculate the Average of a Set of Five Positive
Integers\n");
sum =
0; // initialize sum to 0
count = 0; //
initialize count to 0
// while loop to
input numbers, accumulate sum, and increment count
// Calculate
and output the average
// Prompt user
to end program or continue
System.out.println("Enter a negative number to end the
program.\n");
System.out.println("Enter a nonnegative number to continue."
);
close =
sc.nextInt();
}
}
import java.util.Scanner;
public class Lab5
{
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
final int
MAX_NUMS = 5;
int num; //
variable to store the number
int sum; //
variable to store the sum
double average;
// variable to store the average
int count; //
variable to store the total numbers read
int close; //
loop control variable
close = 1; //
initialize loop control variable
while (close>=0) //check close greater than zero
for user enter negative number to end program then close while
loop
{
System.out.println("Calculate the Average of a Set of
Five Positive Integers");
sum = 0; // initialize sum to 0
count = 0; // initialize count to 0
average = 0; // initialize average to 0
// while loop to input numbers, accumulate sum, and
increment count
System.out.println("Enter Five Positive Integers
numbers = ");
while(MAX_NUMS>count) //check count is less than
MAX_NUMS
{
num = sc.nextInt(); //get user input in num
variable
sum = sum + num; //calculate sum
count++; //increment count
}
//calculate average of numbers
average = sum/count;
System.out.println("Average of Five Positive Integers
is => "+average);
// Prompt user to end program or continue
System.out.println("Enter a negative number to end the
program.");
System.out.println("Enter a nonnegative number to
continue." );
close = sc.nextInt();
}
}
}
===================================OUTPUT==================================
==Please Upvote==