myrdok123

04. Sum of Two Numbers

Jun 22nd, 2024
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. package NestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SumOfTwoNumbers_04 {
  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.  
  15.         boolean flag = false;//при намиране на правилна комбинация променяме стойността на true
  16.  
  17.         for (int i = firstNumber; i <=secondNumber ; i++) {
  18.  
  19.             for (int j = firstNumber; j <= secondNumber ; j++) {
  20.  
  21.                 int currentCombination = i + j;
  22.                 countCombination ++;
  23.  
  24.                 if (currentCombination == magicNumber){
  25.                     System.out.printf("Combination N:%d (%d + %d = %d)", countCombination, i, j, magicNumber);
  26.                     flag = true;
  27.                     break;
  28.                 }
  29.  
  30.             }
  31.  
  32.             if (flag){
  33.                 break;
  34.             }
  35.  
  36.         }
  37.  
  38.         if (!flag){
  39.             System.out.printf("%d combinations - neither equals %d", countCombination, magicNumber);
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment