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
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
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 test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Write a program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT