Advertisement
damesova

Math Puzzle - Java

Dec 16th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. package Exan1to2Dec18;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MathPuzzle {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int key = Integer.parseInt(scanner.nextLine());
  10. int counter = 0;
  11.  
  12. for (int a = 1; a <= 30; a++) {
  13. for (int b = 1; b <= 30; b++) {
  14. for (int c = 1; c <= 30; c++) {
  15.  
  16. if (a + b + c == key) {
  17. if (a < b && b < c) {
  18. System.out.printf("%d + %d + %d = %d%n", a, b, c, key);
  19. }
  20. } else if (a * b * c == key) {
  21. if (a > b && b > c) {
  22. System.out.printf("%d * %d * %d = %d%n", a, b, c, key);
  23. }
  24. } else {
  25. counter++;
  26.  
  27. }
  28. }
  29. }
  30. }
  31. if (counter >= 30 * 30 * 30) {
  32. System.out.println("No!");
  33. }
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement