Advertisement
myrdok123

04. Sum of Two Numbers

Apr 13th, 2024
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 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 firstNumber = Integer.parseInt(scanner.nextLine());
  10.         int secondNumber = Integer.parseInt(scanner.nextLine());
  11.         int magicNumber = Integer.parseInt(scanner.nextLine());
  12.  
  13.         int countCombination = 0;
  14.         boolean flag = false;
  15.  
  16.         for (int first = firstNumber; first <= secondNumber ; first++) {
  17.  
  18.             for (int second = firstNumber; second <= secondNumber ; second++) {
  19.                 countCombination++;
  20.                 int currentCombination = first + second;
  21.  
  22.                 if (currentCombination == magicNumber){
  23.                     System.out.printf("Combination N:%d (%d + %d = %d)", countCombination, first, second, magicNumber);
  24.                     flag = true;
  25.                     break;
  26.                 }
  27.  
  28.             }
  29.  
  30.             if(flag){
  31.                 break;
  32.             }
  33.  
  34.         }
  35.         if(!flag){
  36.             System.out.printf("%d combinations - neither equals %d", countCombination, magicNumber);
  37.         }
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement