Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class BinaryDigitsCount
- {
- static void Main()
- {
- //Telerik Academy
- //You are given a sequence of N positive integer numbers and one binary digit B (0 or 1).
- //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.
- char binaryDigit = char.Parse(Console.ReadLine());
- int numbersN = int.Parse(Console.ReadLine());
- uint binaryDigitCounter = 0;
- uint [] resultsArray = new uint [numbersN];
- for (int counter = 1; counter <= numbersN; counter++)
- {
- binaryDigitCounter = 0;
- uint inputNum = uint.Parse(Console.ReadLine());
- string stringNum = Convert.ToString(inputNum,2);
- foreach (char item in stringNum)
- {
- if (item == binaryDigit)
- {
- binaryDigitCounter++;
- }
- }
- resultsArray[counter - 1] = binaryDigitCounter;
- }
- foreach (uint item in resultsArray)
- {
- Console.WriteLine(item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement