Punkbastard

Return number of even digits in integer - C#

Nov 17th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.64 KB | None | 0 0
  1.         // Return numbers of even digits in n
  2.         public static int NumEvenDigits(int n)
  3.         {
  4.             // counter for even numbers
  5.             int count = 0;
  6.  
  7.             // 0 is a even number so return 1;
  8.             if (n == 0)
  9.             {
  10.                 return 1;
  11.             }
  12.  
  13.             while (n != 0)
  14.             {
  15.                 // if last digit of n is even, add 1 to count
  16.                 if (Math.Abs(n % 10)%2 == 0)
  17.                 {
  18.                     count++;
  19.                 }
  20.                 // remove last digit from n
  21.                 n /= 10;
  22.             }
  23.             return count;
  24.         }
Add Comment
Please, Sign In to add comment