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

Java Calculator with File Output

The document contains a Java program that performs basic arithmetic operations based on user input. It prompts the user for two numbers and an operator, calculates the result, and handles division by zero errors. If the operation is valid, it writes the result to a file named 'example.txt'.

Uploaded by

Faltu
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)
5 views3 pages

Java Calculator with File Output

The document contains a Java program that performs basic arithmetic operations based on user input. It prompts the user for two numbers and an operator, calculates the result, and handles division by zero errors. If the operation is valid, it writes the result to a file named 'example.txt'.

Uploaded by

Faltu
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

Name – Atharva Kulkarni

Roll No – 07

Class – SE(A)

import [Link];

import [Link];

import [Link];

class Write {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

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

double num1 = [Link]();

[Link]("Enter operator (+, -, *, /, %): ");

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

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

double num2 = [Link]();

double result = 0;

boolean validOperation = true;

switch (operator) {

case '+':

result = num1 + num2;

[Link]("Result: " + result);

break;

case '-':
result = num1 - num2;

[Link]("Result: " + result);

break;

case '*':

result = num1 * num2;

[Link]("Result: " + result);

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

[Link]("Result: " + result);

} else {

[Link]("Error: Division by zero is not allowed.");

validOperation = false;

break;

default:

[Link]("Invalid operator! Please use +, -, *, /, or %.");

validOperation = false;

if (validOperation) {

try (FileWriter fout = new FileWriter("[Link]")) {

[Link]("Result: " + result);

} catch (IOException e) {

[Link]("An error occurred while writing to the file: " +

[Link]());

[Link]();
}

You might also like