w3resource

C#: Decimal to Binary


C# Sharp Basic Data Types: Exercise-11 with Solution

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.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: 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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/data-types/csharp-data-type-exercise-11.php