Q. Write an algorithm and program to convert decimal number to binary number.

Solution : Given a number and our task is to convert given decimal number into equivalent binary number.

Binary Number :- A binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols: typically "0" (zero) and "1" (one).
Decimal Number :- A decimal number can be defined as a whole number.

Example :-
Input : 10
Output : 1010

Algorithm For Decimal to Binary Conversion

//Algorithm for Decimal to Binary Conversion.
START
Step 1 ->  Take numbere as input num.
Step 2 ->  while num is greater than 0:
bin[count]=num%2;
num=num/2;
count++;
Step 3 ->  Print bin[] array.                
STOP


Program for Decimal to Binary Conversion

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program for Decimal to Binary Conversion.
    #include <stdio.h>
    int main()
    {
    int num=5,count=0,i;
    int bin[32];
    while(num>0)
    {
    bin[count]=num%2;
    num=num/2;
    count++;
    }
    printf("Binary value is: ");
    for(i=(count-1); i>=0;i--)
    printf("%d",bin[i]);
    return 0;
    }
    
    //C++ Program for Decimal to Binary Conversion.
    #include <iostream>
    using namespace std; 
    int main() 
    { 
    int num=5,count=0,i;
    int bin[32];
    while(num>0)
    {
    bin[count]=num%2;
    num=num/2;
    count++;
    }
    cout<<"Binary value is: ";
    for(i=(count-1); i>=0;i--)
    cout<<bin[i];
    return 0; 
    }
    
    //Java Program for Decimal to Binary Conversion.
    public class LFC
    {
    public static void main(String[] args) {
    int num=5,count=0,i;
    int[] bin = new int[32]; 
    while(num>0)
    {
    bin[count]=num%2;
    num=num/2;
    count++;
    }
    System.out.print("Binary value is: ");
    for(i=(count-1); i>=0;i--)
    System.out.print(bin[i]);
    }
    }
    
    //Python Program for Decimal to Binary Conversion.
    num = 5;
    bin = [0] * num;
    count = 0; 
    while (num > 0):  
    bin[count] = num % 2; 
    num = int(num / 2); 
    count += 1; 
    print("Binary value is: ")    
    for i in range(count - 1, -1, -1): 
    print(bin[i], end = "");   
    
    //C# Program for Decimal to Binary Conversion.
    using System;
    class LFC {
    static void Main() {
    int num = 5;
    int[] bin = new int[32]; 
    int count = 0; 
    while (num > 0) { 
    bin[count] = num % 2; 
    num = num / 2; 
    count++; 
    } 
    Console.Write("Binary value is: ");
    for (int i = count - 1; i >= 0; i--) 
    Console.Write(bin[i]);
    }
    }
    
    //PHP Program for Decimal to Binary Conversion.
    <?php
    $num=5;
    $bin; 
    $count = 0; 
    while ($num > 0)  
    { 
    $bin[$count] = $num % 2; 
    $num = (int)($num / 2); 
    $count++; 
    } 
    echo "Binary value is: ";
    for ($i = $count - 1; $i >= 0; $i--) 
    echo $bin[$i]; 
    ?>
    //C Program for Decimal to Binary Conversion.
    #include <stdio.h>
    int main()
    {
    int num=5,count=0,i;
    int bin[32];
    while(num>0)
    {
    bin[count]=num%2;
    num=num/2;
    count++;
    }
    printf("Binary value is: ");
    for(i=(count-1); i>=0;i--)
    printf("%d",bin[i]);
    return 0;
    }
    

    Output

    Binary value is: 101
    

    Recommended Programs

       Program to print lower triangular elements of an array.
       Program to print full pyramid pattern using stars.