Advertisement
desislava_topuzakova

TwoNumbersSum

Oct 24th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class NumbersSum {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int start_interval = Integer.parseInt(scanner.nextLine());
  7.         int end_interval = Integer.parseInt(scanner.nextLine());
  8.         int magic_number = Integer.parseInt(scanner.nextLine());
  9.         int counter = 0;
  10.         int sum = 0;
  11.         int m = 0;
  12.         int n = 0;
  13.         int counter_end = 0;
  14.         boolean isEqual = false;
  15.         for (int i = start_interval; i >= end_interval; i--) {
  16.             for (int j = start_interval; j >= end_interval; j--) {
  17.  
  18.                 sum = i + j;
  19.                 counter++;
  20.                 if (magic_number == sum) {
  21.                     counter_end = counter++;
  22.                     m = i;
  23.                     n = j;
  24.                     isEqual = true;
  25.                 }
  26.  
  27.             }
  28.         }
  29.  
  30.  
  31.         if (isEqual) {
  32.             System.out.println("Combination N:" + counter_end);
  33.             System.out.println("(" + m + " + " + n + " = " + magic_number + ")");
  34.         } else {
  35.             System.out.println(counter + " combinations - neither equals " + magic_number);
  36.         }
  37.  
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement