Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Calculator

CSE1102 Project 3, Spring 2014


Mayur Shah
2/9/14
TA: Saahil Moledina
Section: 007L
Instructor: Jeffrey A. Meunier

Intro:
This assignment will have you write a program that works like a calculator. The user will
be able to enter numbers and select the desired arithmetic operations. The program will
repeat until the user chooses to exit the program.

Output:

Source Code:
import java.util.Scanner;
//
//
//
//

Calculator
CSE1102
Mayur Shah
2/7/14

public class Calculator {


public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
boolean contin = true;
{

double accumulator = 0.0;


double input;
while (contin)
{
int choice;
System.out.println("0) Exit");

System.out.println("1) Addition");
System.out.println("2) Subtraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Square Root");
System.out.println("6) Clear");
System.out.println("What is Your Choice? ");
choice = kbd.nextInt();
// This is the basic setup for the menu and the list
if (choice == 0)
{
contin = false;
}
else if (choice == 1)
{
System.out.print("Enter a number: ");
input = kbd.nextFloat();
accumulator = accumulator + input;
System.out.println(accumulator);
}
else if (choice == 2)
{
System.out.print("Enter a number: ");
input = kbd.nextFloat();
accumulator = accumulator - input;
System.out.println(accumulator);
}
else if (choice == 3)
{
System.out.print("Enter a number: ");
input = kbd.nextFloat();
accumulator = accumulator * input;
System.out.println(accumulator);
}
else if (choice == 4)
{
System.out.print("Enter a number: ");
input = kbd.nextFloat();
accumulator = accumulator / input;
System.out.println(accumulator);
}
else if (choice == 5)
{
accumulator = Math.sqrt(accumulator);
System.out.println(accumulator);

}
else if (choice == 6)
{
accumulator = 0;
System.out.println(accumulator);
}
else
{

System.out.println("Illegal Command");
// The main section of the program which does calculations.
}
System.out.println("Accumulator = " + accumulator);

}
kbd.close();
// Closes scanner.
}
}

You might also like