0% found this document useful (0 votes)
4 views3 pages

Java Calc PDF

The document outlines an assignment to create a robust Java calculator program that handles user input, processes mathematical operations, and includes error handling. The provided code implements functions for addition, subtraction, multiplication, and division, along with a user interface for selecting operations. The program ensures that division by zero is handled gracefully and allows users to exit the calculator when desired.

Uploaded by

24118048
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)
4 views3 pages

Java Calc PDF

The document outlines an assignment to create a robust Java calculator program that handles user input, processes mathematical operations, and includes error handling. The provided code implements functions for addition, subtraction, multiplication, and division, along with a user interface for selecting operations. The program ensures that division by zero is handled gracefully and allows users to exit the calculator when desired.

Uploaded by

24118048
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

Assignment 1:

Implement a robust Java calculator program that captures user input dynamically,
processes mathematical expressions using conditional logic and looping constructs,
and ensures efficient error handling.

#code:
import [Link];

public class C1 {

// Function for addition


static double add(double a, double b) {
return a + b;
}

// Function for subtraction


static double subtract(double a, double b) {
return a - b;
}

// Function for multiplication


static double multiply(double a, double b) {
return a * b;
}

// Function for division


static double divide(double a, double b) {
if (b == 0) {
[Link]("Error: Cannot divide by zero.");
return 0;
}
return a / b;
}

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


int choice = 0;

while (choice != 5) {

[Link]("\n=== Simple Calculator ===");


[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");
[Link]("5. Exit");
[Link]("Enter your choice: ");

choice = [Link]();

if (choice == 5) {
[Link]("Calculator Closed.");
break;
}

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


double num1 = [Link]();

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


double num2 = [Link]();

double result = 0;

if (choice == 1) {
result = add(num1, num2);
}
else if (choice == 2) {
result = subtract(num1, num2);
}
else if (choice == 3) {
result = multiply(num1, num2);
}
else if (choice == 4) {
result =cd divide(num1, num2);
}
else {
[Link]("Invalid choice!");
continue;
}
[Link]("Result: " + result);
}

[Link]();
}
}

#output:

You might also like