Advertisement
Guest User

Balanced numbers

a guest
Aug 21st, 2022
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         Scanner scanner = new Scanner(System.in);
  6.         int output = 0;
  7.  
  8.         while (true) {
  9.             int input = scanner.nextInt();
  10.             if (input > 99 && input < 1000 && isBalanced(input)) {
  11.                 output += input;
  12.             }
  13.         }
  14.         System.out.println(output);                     // Unreachable statement ???
  15.     }
  16.  
  17.     public static boolean isBalanced(int a) {        // a = 132
  18.         int first =  (a - a % 100) / 100;           //1
  19.         int second = (a % 100) / 10;               //3
  20.         int third =  a % 10;                      //2
  21.  
  22.         if (first+third == second){
  23.             return true;
  24.         }
  25.         return false;
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement