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...
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....
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: ----------------------...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT