Advertisement
Spocoman

09. Sum of Two Numbers

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