Advertisement
veronikaaa86

04. Sum of Two Numbers

Jun 11th, 2022
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 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());
  10. int endNum = Integer.parseInt(scanner.nextLine());
  11. int magicNum = Integer.parseInt(scanner.nextLine());
  12.  
  13. int count = 0;
  14. boolean isValid = false;
  15. for (int i = startNum; i <= endNum; i++) {
  16. for (int j = startNum; j <= endNum; j++) {
  17. count++;
  18. int currentSum = i + j;
  19. if (currentSum == magicNum) {
  20. System.out.printf("Combination N:%d (%d + %d = %d)%n", count, i, j, currentSum);
  21. isValid = true;
  22. break;
  23. }
  24. }
  25. if (isValid) {
  26. break;
  27. }
  28. }
  29.  
  30. if (!isValid) {
  31. System.out.printf("%d combinations - neither equals %d%n", count, magicNum);
  32. }
  33. }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement