Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NestedLoops;
- import java.util.Scanner;
- public class P04SumOfTwoNumbers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int firstNumber = Integer.parseInt(scanner.nextLine());
- int secondNumber = Integer.parseInt(scanner.nextLine());
- int magicNumber = Integer.parseInt(scanner.nextLine());
- int countCombination = 0;
- boolean flag = false;
- for (int first = firstNumber; first <= secondNumber ; first++) {
- for (int second = firstNumber; second <= secondNumber ; second++) {
- countCombination++;
- int currentCombination = first + second;
- if (currentCombination == magicNumber){
- System.out.printf("Combination N:%d (%d + %d = %d)", countCombination, first, second, magicNumber);
- flag = true;
- break;
- }
- }
- if(flag){
- break;
- }
- }
- if(!flag){
- System.out.printf("%d combinations - neither equals %d", countCombination, magicNumber);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement