w3resource

C#: Decimal to Binary


Write a C# Sharp program that takes a decimal number as input and displays its equivalent in binary form.

C# Sharp: Decimal to Binary

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise10
{
    public static void Main()
    {
        // Declare variables to store two numbers and a boolean to determine if both are even
        int n1, n2;
        bool bothEven;

        // Input the first number
        Console.Write("Input First number: ");
        n1 = Convert.ToInt32(Console.ReadLine());

        // Input the second number
        Console.Write("Input Second number: ");
        n2 = Convert.ToInt32(Console.ReadLine());

        // Check if both numbers are even using logical operators
        bothEven = ((n1 % 2 == 0) && (n2 % 2 == 0)) ? true : false;

        // Output the result based on whether both numbers are even or not
        Console.WriteLine(bothEven ? "There're numbers both even" : "There's a number odd");
    }
}

Sample Output:

Input a Number : 65                                                                                           
Binary: 1000001 

Flowchart:

Flowchart: Decimal to Binary.

Go to:


PREV : Write a C# Sharp program that takes two numbers as input and returns true or false when both numbers are even or odd.
NEXT : C# Sharp Conditional Statement Exercises

C# Sharp Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.