Advertisement
veronikaaa86

Two Numbers

Nov 2nd, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class P06_TwoNumbers {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int first = Integer.parseInt(scanner.nextLine());
  8.         int last = Integer.parseInt(scanner.nextLine());
  9.         int magic = Integer.parseInt(scanner.nextLine());
  10.  
  11.         int sum = 0;
  12.         int counter = 0;
  13.         int one = 0;
  14.         int two = 0;
  15.  
  16.         for (int i = first; i >= last; i--) {
  17.             for (int j = first; j >= last; j--) {
  18.                 counter++;
  19.                 if ((i + j) == magic) {
  20.                     sum = i + j;
  21.                     one = i;
  22.                     two = j;
  23.  
  24.                     break;
  25.                 }
  26.             }
  27.             if (sum == magic) {
  28.                 break;
  29.             }
  30.         }
  31.         if (sum==magic) {
  32.             System.out.printf("Combination N:%d (%d + %d = %d)", counter, one, two, magic);
  33.         } else {
  34.             System.out.printf("%d combinations - neither equals %d", counter, magic);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement