The Program in C# Program to Count number of 1’s and 0’s in Binary Representation is given below:
using System;
namespace BinaryCount
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, enter the Number here to count number of Ones and Zeroes :");
int number = int.Parse(Console.ReadLine());
int ones = 0;
int zeros = 0;
while (number > 0)
{
if ((number & 1) == 1)
{
ones++;
}
else
{
zeros++;
}
number = number >> 1;
}
Console.WriteLine("Okay, the number of ones is: " + ones);
Console.WriteLine("Okay, the number of zeros is: " + zeros);
}
}
}
Output:
Hello Codeauri Family, enter the Number here to count number of Ones and Zeroes :Okay, the number of ones is: 4
Okay, the number of zeros is: 3
Pro-Tips💡
Here are the step by step execution of above program:
using System;
: This line includes theSystem
namespace which contains many basic classes and data types that are commonly used in C# programs.namespace BinaryCount
: A namespace is a way to organize code and prevent naming collisions between different classes and namespaces. This line declares theBinaryCount
namespace for the code in this file.class Program
: This line declares a new class calledProgram
. In C#, a class is a blueprint for objects that define their behavior and state.static void Main(string[] args)
: This line declares theMain
method, which is the entry point of the program. Thestatic
keyword means that the method can be called without creating an instance of the class, and thevoid
keyword means that the method doesn’t return a value. Theargs
parameter is an array of strings that can be passed to the program as arguments when it is executed.int number = int.Parse(Console.ReadLine());
: This line reads a string from the user and converts it to an integer. The value is stored in thenumber
variable.int ones = 0;
andint zeros = 0;
: These lines declare two variables,ones
andzeros
, which will be used to count the number of ones and zeros in the binary representation ofnumber
.while (number > 0)
: This line starts a loop that will continue as long asnumber
is greater than 0.if ((number & 1) == 1)
: This line checks if the last bit of the binary representation ofnumber
is 1. The&
operator performs a bitwise AND operation, which returns 1 if both the first and the second operands are 1. In this case, ifnumber & 1
is equal to 1, then the last bit ofnumber
is 1.ones++
: If the last bit ofnumber
is 1, thenones
is incremented by 1.zeros++
: If the last bit ofnumber
is 0, thenzeros
is incremented by 1.number = number >> 1;
: This line shifts the binary representation ofnumber
one bit to the right. This discards the last bit and updates the value ofnumber
for the next iteration of the loop.Console.WriteLine("Okay, the number of ones is: " + ones);
andConsole.WriteLine("Okay, the number of zeros is: " + zeros);
: These lines write the final counts of ones and zeros to the console. The+
operator is used to concatenate the string “Okay, the number of ones is: ” with the value ofones
, and the same for zeros.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.