-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path42.1 Binary Operation Using Lambda Operation.java
79 lines (63 loc) · 1.68 KB
/
42.1 Binary Operation Using Lambda Operation.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Ram's teacher is giving the task to Ram to do the basic arithmetic opertions on two integer values.At a time he can do only one operation. Help the Ram to do the same using Lambda expression.
Input Format
First line will contain two ineteger values on which we have to perform the operation
Second line will contain one integer value which will describe the operation
1 addition
2 subtraction
3 multiplication
4 division
Constraints
number should be integer value
Output Format
one integer value represnting result of the operation if wrong input then print Invalid
Sample Input 0
4 5
2
Sample Output 0
-1
Sample Input 1
3 7.9
1
Sample Output 1
Invalid
*/
import java.util.Scanner;
import java.util.function.BinaryOperator;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a=0,b=0,op=5;
try
{
a = sc.nextInt();
b = sc.nextInt();
op = sc.nextInt();
}
catch(Exception e)
{
System.out.print("Invalid");
System.exit(0);
}
BinaryOperator<Integer> operation;
switch (op) {
case 1:
operation = (x, y) -> x + y;
break;
case 2:
operation = (x, y) -> x - y;
break;
case 3:
operation = (x, y) -> x * y;
break;
case 4:
operation = (x, y) -> x / y;
break;
default:
System.out.println("Invalid");
return;
}
int result = operation.apply(a, b);
System.out.println(result);
}
}