Advertisement
veronikaaa86

04. Sum of Two Numbers

Nov 6th, 2021
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. package nestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04SumOfTwoNumbers {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int startNum = Integer.parseInt(scanner.nextLine());//1
  10. int endNum = Integer.parseInt(scanner.nextLine());//10
  11. int magicNum = Integer.parseInt(scanner.nextLine());
  12.  
  13. boolean isValidComb = false;
  14. int count = 0;
  15. for (int i = startNum; i <= endNum; i++) {
  16. for (int j = startNum; j <= endNum; j++) {
  17. count++;
  18.  
  19. int sum = i + j;
  20.  
  21. if (sum == magicNum) {
  22. isValidComb = true;
  23.  
  24. System.out.printf("Combination N:%d (%d + %d = %d)%n",
  25. count, i, j, sum);
  26. break;
  27. }
  28. }
  29. if (isValidComb) {
  30. break;
  31. }
  32. }
  33.  
  34. if (!isValidComb) {
  35. System.out.printf("%d combinations - neither equals %d", count, magicNum);
  36. }
  37.  
  38. }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement