Advertisement
myrdok123

02. Equal Sums Even Odd Position

Jun 4th, 2023
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. package L06_NestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P02_SumsEvenOddPosition {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int firstNumber = Integer.parseInt(scanner.nextLine());
  10.         int secondNumber = Integer.parseInt(scanner.nextLine());
  11.  
  12.  
  13.         //правим фор цикъл -> преминаваме през всички числа от първото до последното
  14.  
  15.         for (int currentNumber = firstNumber; currentNumber <= secondNumber ; currentNumber++) {
  16.  
  17.             //намираме първото, второто... шестото число
  18.             int firstDigit = currentNumber / 100000;
  19.             int secondDigit = currentNumber / 10000 % 10;
  20.             int thirdDigit = currentNumber / 1000 % 10;
  21.             int fourthDigit = currentNumber / 100 % 10;
  22.             int fifthDigit = currentNumber / 10 % 10;
  23.             int sixthDigit = currentNumber % 10;
  24.  
  25.             int oddSum = firstDigit + thirdDigit + fifthDigit;
  26.             int evenSum = secondDigit + fourthDigit + sixthDigit;
  27.  
  28.             //проверяваме дали сумата на четните е равна на сумата на нечетните
  29.             if(oddSum == evenSum){
  30.                 System.out.printf("%d ", currentNumber);
  31.             }
  32.  
  33.  
  34.  
  35.         }
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement