Advertisement
mmayoub

count even digits

Mar 10th, 2023
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.28 KB | Source Code | 0 0
  1. public static int digits(int n) {
  2.         int count1 = 0;
  3.         int count2 = 0;
  4.  
  5.         while (n > 0) {
  6.             int d = n % 10;
  7.             n /= 10;
  8.  
  9.             if (d % 2 == 0)
  10.                 count2++;
  11.             else
  12.                 count1++;
  13.         }
  14.  
  15.         if (count1 == count2)
  16.             return 0;
  17.  
  18.         if (count1 > count2)
  19.             return 1;
  20.  
  21.         return 2;
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement