-
Notifications
You must be signed in to change notification settings - Fork 109
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
Showing
1 changed file
with
43 additions
and
0 deletions.
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,43 @@ | ||
/* | ||
Write a Java program to implement a simple calculator. The program should prompt the user to enter two numbers and an | ||
operator (+, -, *, or /) and then perform the corresponding operation and display the result. | ||
For example, if the user enters 4, 5, and +, the program should display 9 as the result. Similarly, if the user enters 10, 3, | ||
and *, the program should display 30 as the result. | ||
Your program should handle invalid inputs gracefully, for example, if the user enters an operator that is not one of the four | ||
allowed operators or if the user enters non-numeric inputs. | ||
*/ | ||
import java.util.*; | ||
public class Calculator { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.print("Enter 1st number: "); | ||
int a = sc.nextInt();sc.nextLine(); | ||
System.out.print("Enter 2nd number: "); | ||
int b = sc.nextInt();sc.nextLine(); | ||
System.out.print("Enter operator: "); | ||
char op = sc.nextLine().charAt(0); | ||
|
||
if(op == '+') | ||
{ | ||
System.out.println(a+" "+op+" "+b+" = "+(a+b)); | ||
} | ||
else if(op == '-') | ||
{ | ||
System.out.println(a+" "+op+" "+b+" = "+(a-b)); | ||
} | ||
else if(op == '*') | ||
{ | ||
System.out.println(a+" "+op+" "+b+" = "+(a*b)); | ||
} | ||
else if(op == '/') | ||
{ | ||
System.out.println(a+" "+op+" "+b+" = "+(a/b)); | ||
} | ||
else | ||
{ | ||
System.out.println("Invalid operator"); | ||
} | ||
} | ||
} |