Question

In: Computer Science

Java Program Problem // Description: Read five positive integers and average them. // Enter a negative...

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();     
                                                          
}
}

Solutions

Expert Solution

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==


Related Solutions

Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum and secondNum. The program then prints all palindromic primes (Links to an external site.) between firstNum and secondNum, inclusive. A palindromic number is a number whose digits are the same forward or backward (e.g., 12321). A palindromic prime is a prime number that is also a palindromic number (e.g., 101). You must implement and use the following functions as prototyped below: /// --------------------------------------------------------------- ///...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
java Problem 3: An Interesting Problem Write a program that accepts two positive integers: a deposited...
java Problem 3: An Interesting Problem Write a program that accepts two positive integers: a deposited amount of money and an interest rate, as an annual percentage rate. Your program will calculate the number of years that will take for the account balance to reach $1, 000,000. You can assume that the initial deposit is less than $1,000,000 Input The input will begin with a single line containing T , the number of test cases to follow. The remaining lines...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
A matlab program to enter N numbers and count the positive, negative and zero numbers. Then...
A matlab program to enter N numbers and count the positive, negative and zero numbers. Then print the results
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT