Lab Description:
The objective of this lab is to create a program that will ask user to enter numbers to compute statistics. This program will start by display a menu to ask user to enter a number or exit. The program will ask user to enter as many number as wished, either positive or negative. The result of the program should output like following:
First number entered
Last number entered
Count of number entered
Lowest two numbers
Highest two numbers
Total Sum
Average
Code:
package lab1; import java.util.Scanner; public class Lab1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); //declaration int count=1; int num=0; int max=0; int min = 0; int first=0; int last=1; double sum=0; double avg=0; System.out.print("Enter any amount of numbers:\n"); System.out.println("Enter 0 to exit"); first = input.nextInt(); min = num; //while loop while(true){ num = input.nextInt(); if (num ==0) break; if (num >=max) { max = num; if(first>=max){ max=first; } } if ((num < first) && (num !=0)){ min = num; } else if (num =0){ last=num; } sum=num+sum; count++; } sum = sum + first; avg= sum/count; System.out.printf("the First number is: %d\n",first); System.out.printf("the Last number is: %d\n", last); System.out.printf("the Count of number entered %d\n",count); System.out.printf("the Minimum: %d\n",min); System.out.printf("the Maximum: %d\n",max); System.out.printf("the Sum of all numbers is: "+sum+ "\n"); System.out.printf("the Average of total numbers is: "+avg); input.close(); } }