Advertisement
deyanivanov966

04. Sum of Two Numbers

Apr 5th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Demo {
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         int starIndex = Integer.parseInt(scan.nextLine());
  6.         int endIndex = Integer.parseInt(scan.nextLine());
  7.         int magicalNum = Integer.parseInt(scan.nextLine());
  8.         int counter = 0;
  9.         int firstNum = 0;
  10.         int secondNum = 0;
  11.         boolean hasMagicalNum = false;
  12.         for (firstNum = starIndex; firstNum <= endIndex; firstNum++) {
  13.             for (secondNum = starIndex; secondNum <= endIndex; secondNum++) {
  14.                 counter++;
  15.                 if (firstNum + secondNum == magicalNum) {
  16.                     hasMagicalNum = true;
  17.                     break;
  18.                 }
  19.             }
  20.             if (hasMagicalNum) {
  21.                 break;
  22.             }
  23.         }
  24.         if (hasMagicalNum) {
  25.             System.out.printf("Combination N:%d (%d + %d = %d)",
  26.                     counter, firstNum, secondNum, magicalNum);
  27.         }else {
  28.             System.out.printf("%d combinations - neither equals %d",
  29.                     counter, magicalNum);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement