Basic Programming C# Examples

C# Program to Count number of 1’s and 0’s in Binary Representation

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:

  1. using System;: This line includes the System namespace which contains many basic classes and data types that are commonly used in C# programs.
  2. namespace BinaryCount: A namespace is a way to organize code and prevent naming collisions between different classes and namespaces. This line declares the BinaryCount namespace for the code in this file.
  3. class Program: This line declares a new class called Program. In C#, a class is a blueprint for objects that define their behavior and state.
  4. static void Main(string[] args): This line declares the Main method, which is the entry point of the program. The static keyword means that the method can be called without creating an instance of the class, and the void keyword means that the method doesn’t return a value. The args parameter is an array of strings that can be passed to the program as arguments when it is executed.
  5. 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 the number variable.
  6. int ones = 0; and int zeros = 0;: These lines declare two variables, ones and zeros, which will be used to count the number of ones and zeros in the binary representation of number.
  7. while (number > 0): This line starts a loop that will continue as long as number is greater than 0.
  8. if ((number & 1) == 1): This line checks if the last bit of the binary representation of number 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, if number & 1 is equal to 1, then the last bit of number is 1.
  9. ones++: If the last bit of number is 1, then ones is incremented by 1.
  10. zeros++: If the last bit of number is 0, then zeros is incremented by 1.
  11. number = number >> 1;: This line shifts the binary representation of number one bit to the right. This discards the last bit and updates the value of number for the next iteration of the loop.
  12. Console.WriteLine("Okay, the number of ones is: " + ones); and Console.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 of ones, 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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X