Advertisement
sylviapsh

Binary Digits Count

Dec 27th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. class BinaryDigitsCount
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //You are given a sequence of N positive integer numbers and one binary digit B (0 or 1).
  8.     //Your task is to write a program that finds the number of binary digits (B) in each of the N numbers in binary numeral system. Example: 20 in the binary numeral system looks like this: 10100. The number of binary digits 0 of the number 20 in the binary numeral system is 3.
  9.  
  10.     char binaryDigit = char.Parse(Console.ReadLine());
  11.     int numbersN = int.Parse(Console.ReadLine());
  12.     uint binaryDigitCounter = 0;
  13.     uint [] resultsArray = new uint [numbersN];
  14.  
  15.     for (int counter = 1; counter <= numbersN; counter++)
  16.     {
  17.       binaryDigitCounter = 0;
  18.       uint inputNum = uint.Parse(Console.ReadLine());
  19.       string stringNum = Convert.ToString(inputNum,2);
  20.       foreach (char item in stringNum)
  21.       {
  22.         if (item == binaryDigit)
  23.         {
  24.           binaryDigitCounter++;
  25.         }
  26.       }
  27.       resultsArray[counter - 1] = binaryDigitCounter;
  28.     }
  29.  
  30.     foreach (uint item in resultsArray)
  31.     {
  32.       Console.WriteLine(item);
  33.     }
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement