|
| 1 | +/* |
| 2 | + |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | +Input Format |
| 7 | + |
| 8 | +First line will contain two ineteger values on which we have to perform the operation |
| 9 | +Second line will contain one integer value which will describe the operation |
| 10 | +1 addition |
| 11 | +2 subtraction |
| 12 | +3 multiplication |
| 13 | +4 division |
| 14 | + |
| 15 | +Constraints |
| 16 | + |
| 17 | +number should be integer value |
| 18 | + |
| 19 | +Output Format |
| 20 | + |
| 21 | +one integer value represnting result of the operation if wrong input then print Invalid |
| 22 | + |
| 23 | +Sample Input 0 |
| 24 | + |
| 25 | +4 5 |
| 26 | +2 |
| 27 | +Sample Output 0 |
| 28 | + |
| 29 | +-1 |
| 30 | +Sample Input 1 |
| 31 | + |
| 32 | +3 7.9 |
| 33 | +1 |
| 34 | +Sample Output 1 |
| 35 | + |
| 36 | +Invalid |
| 37 | +*/ |
| 38 | +import java.util.Scanner; |
| 39 | +import java.util.function.BinaryOperator; |
| 40 | + |
| 41 | +public class Main { |
| 42 | + public static void main(String[] args) { |
| 43 | + Scanner sc = new Scanner(System.in); |
| 44 | + int a=0,b=0,op=5; |
| 45 | + try |
| 46 | + { |
| 47 | + a = sc.nextInt(); |
| 48 | + b = sc.nextInt(); |
| 49 | + op = sc.nextInt(); |
| 50 | + } |
| 51 | + catch(Exception e) |
| 52 | + { |
| 53 | + System.out.print("Invalid"); |
| 54 | + System.exit(0); |
| 55 | + } |
| 56 | + BinaryOperator<Integer> operation; |
| 57 | + |
| 58 | + switch (op) { |
| 59 | + case 1: |
| 60 | + operation = (x, y) -> x + y; |
| 61 | + break; |
| 62 | + case 2: |
| 63 | + operation = (x, y) -> x - y; |
| 64 | + break; |
| 65 | + case 3: |
| 66 | + operation = (x, y) -> x * y; |
| 67 | + break; |
| 68 | + case 4: |
| 69 | + operation = (x, y) -> x / y; |
| 70 | + break; |
| 71 | + default: |
| 72 | + System.out.println("Invalid"); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + int result = operation.apply(a, b); |
| 77 | + System.out.println(result); |
| 78 | + } |
| 79 | +} |
0 commit comments