Advertisement
desislava_topuzakova

03. Sum of two numbers

Jan 16th, 2022
787
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package NestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SumOfTwoNumbers_03 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int startNumber = Integer.parseInt(scanner.nextLine());
  9.         int endNumber = Integer.parseInt(scanner.nextLine());
  10.         int magicNumber = Integer.parseInt(scanner.nextLine());
  11.  
  12.         int count = 0; //броя комбинациите
  13.         for (int firstNumber = startNumber; firstNumber <= endNumber; firstNumber++) {
  14.             for (int secondNumber = startNumber; secondNumber <= endNumber; secondNumber++) {
  15.                 //комбинация
  16.                 count++;
  17.                 if (firstNumber + secondNumber == magicNumber) {
  18.                     System.out.printf("Combination N:%d (%d + %d = %d)", count, firstNumber, secondNumber, magicNumber);
  19.                     return;
  20.                 }
  21.             }
  22.         }
  23.         //не сме намерили комбинация
  24.         System.out.printf("%d combinations - neither equals %d", count, magicNumber);
  25.  
  26.     }
  27. }
  28.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement