Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class EqualSumsEvenOddPosition {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int firstNum = Integer.parseInt(scanner.nextLine());
- int secondNum = Integer.parseInt(scanner.nextLine());
- for (int i = firstNum; i <= secondNum; i++) {
- // save current num
- int currentNum = i;
- int evenSum = 0;
- int oddSum = 0;
- for(int j = 0; j < 6; j++) {
- // get last digit from number
- int lastDigit = currentNum % 10;
- // cut last digit, so we can iterate for the next (last) digit
- currentNum = currentNum / 10;
- // even position
- if(j % 2 == 0) {
- evenSum = evenSum + lastDigit;
- }
- // odd position
- else {
- oddSum = oddSum + lastDigit;
- }
- }
- // check if both sums are equal
- if(evenSum == oddSum) {
- System.out.print(i + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement