Advertisement
veronikaaa86

4. Сума от две числа

Feb 11th, 2023
1,449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. package nestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04SumOfTwoNumbers {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int firstNum = Integer.parseInt(scanner.nextLine());
  10.         int secondNum = Integer.parseInt(scanner.nextLine());
  11.         int magicNum = Integer.parseInt(scanner.nextLine());
  12.  
  13.         boolean isValid = false;
  14.         int count = 0;
  15.         for (int i = firstNum; i <= secondNum; i++) {
  16.             for (int j = firstNum; j <= secondNum; j++) {
  17.                 count++;
  18.                 int sum = i + j;
  19.  
  20.                 if (sum == magicNum) {
  21.                     isValid = true;
  22.                     System.out.printf("Combination N:%d%n", count);
  23.                     System.out.printf("(%d + %d = %d)%n", i, j, sum);
  24.                     break;
  25.                 }
  26.             }
  27.             if (isValid) {
  28.                 break;
  29.             }
  30.         }
  31.         if (!isValid) {
  32.             System.out.printf("%d combinations - neither equals %d%n", count, magicNum);
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement