Lab Description:
For this lab experiment, we were given an instruction to create a program that will prompt the user to enter several numbers to compute the statistic.
For example , the program will display the following computations ; minimun, maximun , sum, average , the first and last number entered by the user. More over i used the “0” to terminate the program.:
Source Code:
import java.util.Scanner; public class lab1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner( System.in); int first =0; int last; int minimum = 0; int maximum = 0; int sum = 0; int newnumber; int count = 0; System.out.println("Lab1"); System.out.println("ENTER A NUMBER, TO EXIT ENTER 0"); newnumber = input.nextInt(); while (newnumber != 0) { sum += newnumber; count++; last = newnumber; if (count == 1) { minimum = newnumber; maximum = newnumber; first = newnumber; } if (newnumber < minimum ) { minimum = newnumber; } if (newnumber > maximum) { maximum = newnumber; } if (count%5 == 0) { System.out.println("Minimum is: " + minimum); System.out.println("Maximum is: " + maximum); System.out.println("Average is: " + sum/count); System.out.println("Sum is: " + sum); System.out.println("The first number is: " + first); System.out.println("The last number is: " + last); } System.out.println("Enter a number: To Exit enter 0"); newnumber = input.nextInt(); } } }
Screenshots: