Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class NumberFun {
  5.  
  6. private static boolean addition (int a, int b, int ans) {
  7. int temp;
  8. if (a > b) {
  9. temp = a;
  10. a = b;
  11. b = temp;
  12. }
  13. while (a < b) {
  14. if (a + b == ans) {
  15. return true;
  16. }
  17. else {
  18. a = a+b;
  19. }
  20. }
  21. return false;
  22. }
  23.  
  24. private static boolean subtraction (int a, int b, int ans) {
  25. int temp;
  26. if (a < b) {
  27. temp = a;
  28. a = b;
  29. b = temp;
  30. }
  31. while (a > b) {
  32. if (a - b == ans) {
  33. return true;
  34. }
  35. else {
  36. a = a - b;
  37. }
  38. }
  39. return false;
  40. }
  41. private static boolean multiplication(int a, int b, int ans) {
  42. int temp;
  43. if (a > b) {
  44. temp = a;
  45. a = b;
  46. b = temp;
  47. }
  48.  
  49. while (a < b) {
  50. if (a * b == ans) {
  51. return true;
  52. }
  53. else {
  54. a = a*b;
  55. }
  56. }
  57. return false;
  58.  
  59. }
  60. private static boolean division (int a, int b, int ans) {
  61. int temp;
  62. if (a < b) {
  63. temp = a;
  64. a = b;
  65. b = temp;
  66. }
  67. while (a > b) {
  68. if (a / b == ans) {
  69. return true;
  70. }
  71. else {
  72. a = a/b;
  73. }
  74. }
  75. return false;
  76. }
  77.  
  78. public static void main(String[] args) {
  79.  
  80. Scanner sc = new Scanner(System.in);
  81. int a; int b; int c;
  82. int range = sc.nextInt();
  83. for (int i = 0; i < range; i++) {
  84. a = sc.nextInt();
  85. b = sc.nextInt();
  86. c = sc.nextInt();
  87.  
  88. if (addition(a,b,c) || subtraction(a,b,c) || division(a,b,c) || multiplication(a,b,c)) {
  89. System.out.println("Possible");
  90. }
  91. else {
  92. System.out.println("Impossible");
  93. }
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement