Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Demo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int keyNumber = Integer.parseInt(scanner.nextLine());
- // Reading the keyNumber from the console.
- boolean isInvalid = true;
- // define boolean to change it if there is valid combination
- for (int i = 1; i <= 30; i++) {
- for (int j = 1; j <= 30; j++) {
- for (int k = 1; k <= 30; k++) {
- //create 3 nested loops for the three numbers.
- if ((i < j && j < k) && (i + j + k == keyNumber)) {
- System.out.printf("%d + %d + %d = %d%n", i, j, k, keyNumber);
- // make the checks and print if you find valid combination
- isInvalid = false;
- // change the boolean variable to false.
- }
- if ((i > j && j > k) && (i * j * k == keyNumber)) {
- System.out.printf("%d * %d * %d = %d%n", i, j, k, i * j * k);
- // make checks and print if there is valid combinations
- isInvalid = false;
- // change to false
- }
- }
- }
- }
- if (isInvalid) {
- System.out.println("No!");
- // if the boolean variable "isInvalid" is still true, we haven't met valid combinations
- // so we print No!
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement