Author : Rahul

Write a program to convert decimal number to binary number


Program

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string decimalNumberVal;
     int decimalNumber, binaryNumber = 0, tempNumber, digit, power = 0;

     //User Input
     Console.Write("Enter Decimal Number:");
     decimalNumberVal = Console.ReadLine();
     decimalNumber = Convert.ToInt32(decimalNumberVal);
     
     //Decimal to Binary Logic
     tempNumber = decimalNumber;
     while (tempNumber > 0)
     {
       digit = tempNumber % 2;
       binaryNumber = binaryNumber + digit * (Int32)Math.Pow(10, power);
       power++;
       tempNumber = tempNumber / 2;
     }
     
     //Print Result
     Console.WriteLine($"Binary Number: {binaryNumber}");
     
     Console.Read();
   }
 }
}

Output

Enter Decimal Number:25
Binary Number: 11001