-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
61 lines (57 loc) · 1.8 KB
/
Calculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DAA;
/**
*
* @author sunilshetty07
*/
import java.util.*;
public class Calculator {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
float a,b,res;
int val;
System.out.println("enter the value of a");
a=in.nextFloat();
System.out.println("enter the value of b");
b=in.nextFloat();
System.out.println("1-->add\n2-->subtract\n3-->multiplication\n4-->division\n5-->exponent");
System.out.println("Enter your choice:");
val=in.nextInt();
while (val==0 || val>5)
{
System.out.println("Please enter number between 1 and 5");
val=in.nextInt();
}
switch(val)
{
case 1:res=a+b;
System.out.println("addition of "+a+" and "+b+" is: "+res);
break;
case 2:res=a-b;
System.out.println("Subtraction of "+a+" and "+b+" is: "+res);
break;
case 3:res=a*b;
System.out.println("Multiplication of "+a+" and "+b+" is: "+res);
break;
case 4:
try
{
res=a/b;
System.out.println("result of division is:"+res);
}
catch(ArithmeticException e)
{
System.out.println("Exceptin:"+e);
}
break;
case 5:
res=(int)Math.pow(a, b);
System.out.println(res);
break;
}
}
}