settingsAccountsettings
Menusettings

Q: Hit The Target - Java Task

+4 votes

Write a program that takes as input an integer – the target – and outputs to the console all pairs of numbers between 1 and 20, which, if added or subtracted, result in the target:

hit the target - java task

asked in Java category by user sam
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Very interesting task, my friend! Here is what I have as a solution:

import java.util.Scanner;

public class Pr_09_HitTheTarget {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int result = scanner.nextInt();

        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j <= 20; j++) {
                if (i + j == result) {
                    System.out.println(i + "+" + j + "=" + result);
                }
            }
        }

        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j <= 20; j++) {
                if (i - j == result) {
                    System.out.println(i + "-" + j + "=" + result);
                }
            }
        }
    }
}
answered by user golearnweb
edited by user golearnweb
...