w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: 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 Exercise11
{
  public static void Main()
  {   
    string answer;  
    string result; 
  
         Console.Write("Input a Number : ");
        answer = Console.ReadLine();
         
            int num = Convert.ToInt32(answer);
            result = "";
            while (num > 1)
            {
                int remainder = num % 2;
                result = Convert.ToString(remainder) + result;
                num /= 2;
            }
            result = Convert.ToString(num) + result;
            Console.WriteLine("Binary: {0}", result);
        }
   }
   

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?



New Content: Composer: Dependency manager for PHP, R Programming