Advertisement
LenCristoff

Progblem 06 - Sum of Two Numbers

Aug 24th, 2017
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Demo {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6. int starIndex = Integer.parseInt(scan.nextLine());
  7. int endIndex = Integer.parseInt(scan.nextLine());
  8. int magicalNum = Integer.parseInt(scan.nextLine());
  9. int counter = 0;
  10. int firstNum = 0;
  11. int secondNum = 0;
  12. boolean hasMagicalNum = false;
  13. for (firstNum = starIndex; firstNum <= endIndex; firstNum++) {
  14. for (secondNum = starIndex; secondNum <= endIndex; secondNum++) {
  15. counter++;
  16. if (firstNum + secondNum == magicalNum) {
  17. hasMagicalNum = true;
  18. break;
  19. }
  20. }
  21. if (hasMagicalNum) {
  22. break;
  23. }
  24. }
  25. if (hasMagicalNum) {
  26. System.out.printf("Combination N:%d (%d + %d = %d)",
  27. counter, firstNum, secondNum, magicalNum);
  28. }else {
  29. System.out.printf("%d combinations - neither equals %d",
  30. counter, magicalNum);
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement