Sunday, March 13, 2016

Basic Calculator Java Source Code (2.4)

The code below demonstrates how to create a simple calculator as instructed in Intro to Java section 2.4

package basiccalc;
import java.util.Scanner;
public class Calculator {
    public static void main(String[] args) {
        //Create The Scanner
        Scanner userInput = new Scanner(;
        //User Input
        System.out.println("Welcome to Calculator");
        System.out.println("Please enter your first value:");
        double x = userInput.nextDouble();
        System.out.println("Please enter your second value:");
        double y = userInput.nextDouble();
        //Close Scanner
        double sum = x + y;
        double product = x * y;
        double quotient = x / y;
        double remainder = x % y;
        //Print out the results
        System.out.println("Results are as follows:");
        System.out.printf("Sum: %f + %f = %f \n",x,y,sum);
        System.out.printf("Product: %f * %f = %f \n",x,y,product);
        System.out.printf("Quotient: %f / %f = %f \n",x,y,quotient);
        System.out.printf("Remainder: %f / %f = %f",x,y,remainder);


Post a Comment