-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98d1c92
commit f50c6c3
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import java.util.Scanner; | ||
|
||
public class Calculator { | ||
|
||
// Method to perform addition | ||
public static double add(double num1, double num2) { | ||
return num1 + num2; | ||
} | ||
|
||
// Method to perform subtraction | ||
public static double subtract(double num1, double num2) { | ||
return num1 - num2; | ||
} | ||
|
||
// Method to perform multiplication | ||
public static double multiply(double num1, double num2) { | ||
return num1 * num2; | ||
} | ||
|
||
// Method to perform division | ||
public static double divide(double num1, double num2) { | ||
if (num2 == 0) { | ||
System.out.println("Error! Division by zero is not allowed."); | ||
return 0; | ||
} | ||
return num1 / num2; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.println("Welcome to the Calculator!"); | ||
System.out.println("Enter first number: "); | ||
double num1 = scanner.nextDouble(); | ||
|
||
System.out.println("Enter second number: "); | ||
double num2 = scanner.nextDouble(); | ||
|
||
System.out.println("Choose operation: +, -, *, /"); | ||
char operation = scanner.next().charAt(0); | ||
|
||
double result = 0; | ||
|
||
switch (operation) { | ||
case '+': | ||
result = add(num1, num2); | ||
break; | ||
case '-': | ||
result = subtract(num1, num2); | ||
break; | ||
case '*': | ||
result = multiply(num1, num2); | ||
break; | ||
case '/': | ||
result = divide(num1, num2); | ||
break; | ||
default: | ||
System.out.println("Invalid operation!"); | ||
return; | ||
} | ||
|
||
System.out.println("The result is: " + result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters