Advertisement
veronikaaa86

02. Equal Sums Even Odd PositionV02

Dec 5th, 2021
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. package nestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P02EqualSumsEvenOddPositionV02 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int firstNum = Integer.parseInt(scanner.nextLine());
  10. int secondNum = Integer.parseInt(scanner.nextLine());
  11.  
  12. for (int i = firstNum; i <= secondNum; i++) {
  13. String numberAsString = "" + i;
  14.  
  15. int evenSum = 0;
  16. int oddSum = 0;
  17. for (int j = 0; j < 6; j++) {
  18. int digit = Integer.parseInt("" + numberAsString.charAt(j));
  19.  
  20. if (j % 2 == 0) {
  21. evenSum = evenSum + digit;
  22. } else {
  23. oddSum = oddSum + digit;
  24. }
  25. }
  26.  
  27. if (evenSum == oddSum) {
  28. System.out.print(numberAsString + " ");
  29. }
  30. }
  31. }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement