elena1234

BinaryDigitsCount

Oct 4th, 2020 (edited)
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. using System;
  2.  
  3. namespace BinaryDigitsCount
  4. {
  5.     class MainClass
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             int number = int.Parse(Console.ReadLine());
  10.             int binaryDigit = int.Parse(Console.ReadLine());
  11.             int count = 0;
  12.  
  13.             while (number != 0)
  14.             {
  15.                 int bitReminder = number % 2;// return only 1 or 0
  16.                 if (bitReminder == binaryDigit)
  17.                 {
  18.                     count++;
  19.                 }
  20.                 number = number / 2;// we divide by 2, because binary system base 2
  21.             }
  22.             Console.WriteLine(count);
  23.  
  24.           }
  25.         }
  26.       }
  27.  
  28.  
Add Comment
Please, Sign In to add comment