Advertisement
Spocoman

04. Sum of Two Numbers

Aug 29th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 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 startNum = Integer.parseInt(scanner.next());
  7.         int endNum = Integer.parseInt(scanner.next());
  8.         int magicNum = Integer.parseInt(scanner.next()),
  9.                 counter = 0;
  10.         boolean isMagic = false;
  11.  
  12.         for (int i = startNum; i <= endNum; i++) {
  13.             for (int j = startNum; j <= endNum; j++) {
  14.                 counter++;
  15.                 if (i + j == magicNum) {
  16.                     System.out.printf("Combination N:%d (%d + %d = %d)\n", counter, i, j, magicNum);
  17.                     isMagic = true;
  18.                     break;
  19.                 }
  20.             }
  21.             if (isMagic == true) {
  22.                 break;
  23.             }
  24.         }
  25.  
  26.         if (isMagic == false) {
  27.             System.out.printf("%d combinations - neither equals %d\n", counter, magicNum);
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement