0% found this document useful (0 votes)
6 views

Import Java

Uploaded by

brightonpaul003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Import Java

Uploaded by

brightonpaul003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner; public class Calculator { public static void main(String[] args) { // Create a
Scanner object for user input Scanner scanner = new Scanner(System.in); // Display welcome
message System.out.println("Welcome to the Simple Calculator!"); // Prompt user to enter two
numbers System.out.print("Enter first number: "); double num1 = scanner.nextDouble();
System.out.print("Enter second number: "); double num2 = scanner.nextDouble(); // Prompt user to
choose an operation System.out.println("Choose an operation: +, -, *, /"); char operator =
scanner.next().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 { System.out.println("Error! Division by zero."); return; } break;
default: System.out.println("Invalid operator!"); return; } // Display the result
System.out.println("The result is: " + result);

import java.util.Scanner;

public class Cal {

public static void main(String[] args) {

// Create a Scanner object for user input

Scanner scanner = new Scanner(System.in);

// Display welcome message

System.out.println("Welcome to the Simple Calculator!");

// Prompt user to enter two numbers

System.out.print("Enter first number: ");

double num1 = scanner.nextDouble();

System.out.print("Enter second number: ");

double num2 = scanner.nextDouble();

// Prompt user to choose an operation

System.out.println("Choose an operation: +, -, *, /");

char operator = scanner.next().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 {

System.out.println("Error! Division by zero.");

return;

break;

default:

System.out.println("Invalid operator!");

return;

// Display the result

System.out.println("The result is: " + result);

You might also like