Venciity

[SoftUni C# exam preparation] 04.BinaryDigitsCount

Apr 9th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class BinaryDigitsCount
  8. {
  9.     static void Main()
  10.     {
  11.         byte binaryDigit = Byte.Parse(Console.ReadLine());
  12.         int numbers = Int32.Parse(Console.ReadLine());
  13.         int count = 0;
  14.  
  15.         for (int i = 0; i < numbers; i++)
  16.         {
  17.             long number = long.Parse(Console.ReadLine());
  18.             string binaryNumberString = Convert.ToString(number, 2);
  19.  
  20.             for (int j = 0; j < binaryNumberString.Length; j++)
  21.             {
  22.                 if (byte.Parse(binaryNumberString[j].ToString()) == binaryDigit)
  23.                 {
  24.                     count++;
  25.                 }
  26.             }
  27.             Console.WriteLine(count);
  28.             count = 0;
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment