[Go to site: main page, start]

0% found this document useful (0 votes)
3 views2 pages

Simple Java Calculator Program

Uploaded by

brightonpaul003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Simple Java Calculator Program

Uploaded by

brightonpaul003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

Scanner; public class Calculator { public static void main(String[] args) { // Create a
Scanner object for user input Scanner scanner = new Scanner([Link]); // Display welcome
message [Link]("Welcome to the Simple Calculator!"); // Prompt user to enter two
numbers [Link]("Enter first number: "); double num1 = [Link]();
[Link]("Enter second number: "); double num2 = [Link](); // Prompt user to
choose an operation [Link]("Choose an operation: +, -, *, /"); char operator =
[Link]().charAt(0); double result; // Perform the chosen operation switch (operator) { case '+':
result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2;
break; case '/': // Check if the second number is not zero to avoid division by zero if (num2 != 0)
{ result = num2 / num1; } else { [Link]("Error! Division by zero."); return; } break;
default: [Link]("Invalid operator!"); return; } // Display the result
[Link]("The result is: " + result);

import [Link];

public class Cal {

public static void main(String[] args) {

// Create a Scanner object for user input

Scanner scanner = new Scanner([Link]);

// Display welcome message

[Link]("Welcome to the Simple Calculator!");

// Prompt user to enter two numbers

[Link]("Enter first number: ");

double num1 = [Link]();

[Link]("Enter second number: ");

double num2 = [Link]();

// Prompt user to choose an operation

[Link]("Choose an operation: +, -, *, /");

char operator = [Link]().charAt(0);


double result;

// Perform the chosen operation

switch (operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

// Check if the second number is not zero to avoid division by zero

if (num2 != 0) {

result = num2 / num1;

} else {

[Link]("Error! Division by zero.");

return;

break;

default:

[Link]("Invalid operator!");

return;

// Display the result

[Link]("The result is: " + result);

You might also like