2
29/10/2024 4:50 pm
Début du sujet
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:

1 Réponse
2
29/10/2024 4:51 pm
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);
}
}
}
}
}
