Description:
For our second lab, we had to create a calculator that can find the Minimum, Maximum, Average, Sum, First Number Entered, Last Number Entered, and Total Numbers Entered. This program allows the user to enter as many numbers as he wants.
Code:
package lab2; import java.util.Scanner; public class Lab2Edit { public static void main ( String [] args) { Scanner input = new Scanner (System.in); double a; double b; double c = 0; double first; double last; double max = 0; double min; double avg; double sum; double sumFill = 0; int totalNumber = 1; System.out.println("Enter as many numbers as you want"); System.out.println("To stop, enter 0"); a = input.nextDouble(); if ( a != 0) { first = a; last = a; max = a; min = a; avg = a; sum = a; do { b = input.nextDouble(); if ( b != 0) { last = b; c = b; if ( b > max) max = b; if (c < min) min = c; sumFill = sumFill + b; sum = sumFill + a; totalNumber++; avg = sum / totalNumber; } } while( b != 0 ); System.out.println("Maximum is : " + max); System.out.println("Minimum is : " + min); System.out.println("First number entered is : " + first); System.out.println("Last number entered is : " + last); System.out.println("Total sum is : " +sum); System.out.println("Total numbers entered : " +totalNumber); System.out.println("Average is : " +avg); } else { System.out.println("You did not enter a number"); } input.close(); } }