veronikaaa86

02. Equal Sums Even Odd Position

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