in the future - u will be able to do some more stuff here,,,!! like pat catgirl- i mean um yeah... for now u can only see others's posts :c
JAVA CODE-
code for calculator -
import java.util.Scanner;
public class Calculator {
public static double add(double a, double b) {
return a + b;
}
public static double subtract(double a, double b) {
return a - b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double divide(double a, double b) { if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
} return a / b;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Calculator Program!");
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.println("Select an operation:");
System.out.println("1: Addition");
System.out.println("2: Subtraction");
System.out.println("3: Multiplication");
System.out.println("4: Division");
System.out.print("Enter your choice (1-4): ");
int choice = scanner.nextInt();
double result = 0;
switch (choice) {
case 1:
result = add(num1, num2);
System.out.println("Result of Addition: " + num1 + " + " + num2 + " = " + result);
break;
case 2:
result = subtract(num1, num2); System.out.println("Result of Subtraction: " + num1 + " - " + num2 + " = " + result);
break;
case 3:
result = multiply(num1, num2); System.out.println("Result of Multiplication: " + num1 + " * " + num2 + " = " + result);
break;
case 4:
try { result = divide(num1, num2); System.out.println("Result of Division: " + num1 + " / " + num2 + " = " + result); }
catch (ArithmeticException e) {
System.out.println(e.getMessage()); }
break;
default:
System.out.println("Invalid choice! Please select a number between 1 and 4."); }
scanner.close();
}
}
0 - 0