Advertisement
GabrielHr00

02. Equal Sums Even Odd Position

Feb 9th, 2025
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package nestedLoops_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class EqualSumsEvenOddPosition {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int startNumber = Integer.parseInt(scanner.nextLine());
  9.         int endNumber = Integer.parseInt(scanner.nextLine());
  10.  
  11.  
  12.  
  13.         for (int i = startNumber; i <= endNumber; i++) {
  14.             int currentNum = i;
  15.             int sumOdd = 0;
  16.             int sumEven = 0;
  17.  
  18.             for (int j = 0; j <= 5; j++) {
  19.                 int lastDigit = currentNum % 10;
  20.                 currentNum = currentNum / 10;
  21.  
  22.                 if (j % 2 == 0) {
  23.                     // even
  24.                     sumEven += lastDigit;
  25.                 } else {
  26.                     // odd
  27.                     sumOdd += lastDigit;
  28.                 }
  29.             }
  30.  
  31.             if (sumEven == sumOdd) {
  32.                 System.out.printf("%d ", i);
  33.             }
  34.  
  35.         }
  36.  
  37.  
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement