Write a C# program to convert decimal number into binary number.

Write a C# program to convert decimal number into binary number.

Last updated:11/30/2021 3:35:38 AM

1 Answers

Manish Manish
Manish Manish

Following C# program is to convert the Decimal number into Binary number,

Program:

using System;

public class Program
{
 public static void Main()
 {
    int i=1, binary=0;
    int rem, num;
  Console.WriteLine('Enter a decimal number');
  num=int.Parse(Console.ReadLine());
    while(num!=0)
    {
        rem=num%2;
       num=num/2;
       binary=binary+(rem*i);
       i=i*10;
    }
  Console.WriteLine('Binary number is :'+binary);
 }
}

Output:

Enter a decimal number

20
Binary number is :10100

Answer