0% found this document useful (0 votes)
6 views4 pages

Explanation

This document describes a basic calculator program that takes two numbers from a user, an operator, and performs the calculation, outputting the result. It asks for two numbers, an operator, performs the calculation in a switch statement, and outputs the result formatted to 1 decimal place if it has a decimal, otherwise as a whole number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Explanation

This document describes a basic calculator program that takes two numbers from a user, an operator, and performs the calculation, outputting the result. It asks for two numbers, an operator, performs the calculation in a switch statement, and outputs the result formatted to 1 decimal place if it has a decimal, otherwise as a whole number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Valencia, Mikaella Bianca T.

CCIS-2B
So I use a new Scanner object with `Scanner scanner = new Scanner(System.in) This object

will be used to gather input from the user. Then the code System.out.println("Enter first number:

") and double num1 = scanner.nextDouble() prints a message to the console asking the user to

enter the first number. The Scanner then captures that number from the user. So I use double

to allow the number to be a decimal number. The same process is repeated for the second

number. With System.out.println("Enter an operator (+, -, *, /): ");` and `char operator =

scanner.next().charAt(0) we ask the user to enter an operator. then close the scanner object

with scanner.close() This helps to prevent resource leaks.

The switch statement performs the operation based on the operator the user entered. If the

operator is not one of the four valid operators (+, -, *, /), an error message is printed to the

output.

This program is a calculator. It helps you do calculation computations with two figures. First, it

asks you to enter the first number. You can enter any number with or without a decimal point.

Also, it asks you to choose an operation like adding, multiplying, or dividing. You can use the

symbols,-, *, or / After that, it asks you to enter the alternate number. you can enter any number

you want. Once you have entered all the figures and chosen an operation, the program will do

the calculation for you and show the result. Still, it will show the answer without any decimal

points, If the result is a whole number(no decimal places). But if the result has decimal places, it

will show the answer with one decimal place.


import static java.lang.System.out;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
out.println("Enter first number: ");
double num1 = scanner.nextDouble();

out.println("Enter second number: ");


double num2 = scanner.nextDouble();

out.println("Enter an operator (+, -, *, /): ");


char operator = scanner.next().charAt(0);

scanner.close();
double output;

switch(operator)
{
case '+':
output = num1 + num2;
break;

case '-':
output = num1 - num2;
break;

case '*':
output = num1 * num2;
break;

case '/':
if (num2 != 0) {
output = num1 / num2;
} else {
out.println("Bawal zero makulet.");
return;
}
break;

default:
out.println("mali operator mo.");
return;
}

out.println("The Answer is: ");


if (output == (int) output)
out.printf("%.0f %c %.0f = %.0f", num1, operator, num2, output);
else
out.printf("%.1f %c %.1f = %.1f", num1, operator, num2, output);

}
}

You might also like