Sum the Binary Representation of Integer’s

Write a program to find the sum of given numbers Binary Representation

For example:

Input:
12


Output:
2


Binary form 12 is 1100

Solution:

using System;

namespace Binary
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter Number");
            var n = Convert.ToInt32(Console.ReadLine());

            var sum = 0;
            while (n > 0)
            {
                var reminder = n % 2;
                sum += reminder;
                n = n / 2;
            }

            Console.WriteLine($"Sum of the Binary number is {sum} ");
            Console.ReadLine();
        }

    }
}

Leave a Reply

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