veronikaaa86

03. Combinations

Oct 15th, 2022 (edited)
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 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 start = Integer.parseInt(scanner.nextLine());
  10.         int end = Integer.parseInt(scanner.nextLine());
  11.         int magicNum = Integer.parseInt(scanner.nextLine());
  12.  
  13.         boolean isValidComb = false;
  14.         int count = 0;
  15.         for (int i = start; i <= end; i++) {
  16.             for (int j = start; j <= end; j++) {
  17.                 count++;
  18.                 int sum = i + j;
  19.                 if (sum == magicNum) {
  20.                     System.out.printf("Combination N:%d ", count);
  21.                     System.out.printf("(%d + %d = %d)%n", i, j, magicNum);
  22.                     isValidComb = true;
  23.                     break;
  24.                 }
  25.             }
  26.             if (isValidComb){
  27.                 break;
  28.             }
  29.         }
  30.  
  31.         if (!isValidComb) {
  32.             System.out.printf("%d combinations - neither equals %d", count, magicNum);
  33.         }
  34.     }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment