Advertisement
marking2112

MathPuzzle

Dec 8th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int keyNumber = Integer.parseInt(scanner.nextLine());
  8.         // Reading the keyNumber from the console.
  9.         boolean isInvalid = true;
  10.         // define boolean to change it if there is valid combination
  11.  
  12.         for (int i = 1; i <= 30; i++) {
  13.             for (int j = 1; j <= 30; j++) {
  14.                 for (int k = 1; k <= 30; k++) {
  15.                     //create 3 nested loops for the three numbers.
  16.                     if ((i < j && j < k) && (i + j + k == keyNumber)) {
  17.                         System.out.printf("%d + %d + %d = %d%n", i, j, k, keyNumber);
  18.                         // make the checks and print if you find valid combination
  19.                         isInvalid = false;
  20.                         // change the boolean variable to false.
  21.                     }
  22.                     if ((i > j && j > k) && (i * j * k == keyNumber)) {
  23.                         System.out.printf("%d * %d * %d = %d%n", i, j, k, i * j * k);
  24.                         // make checks and print if there is valid combinations
  25.                         isInvalid = false;
  26.                         // change to false
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.         if (isInvalid) {
  32.             System.out.println("No!");
  33.             // if the boolean variable "isInvalid" is still true, we haven't met valid combinations
  34.             // so we print No!
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement