Program to Convert Decimal to Binary

Program to Convert Decimal to Binary

  • Write a program to Convert Decimal to Binary in C
  • Write a program to Convert Decimal to Binary in C++
  • Write a program to Convert Decimal to Binary in Python
  • Write a program to Convert Decimal to Binary in PHP
  • Write a program to Convert Decimal to Binary in Java
  • Write a program to Convert Decimal to Binary in Java Script
  • Write a program to Convert Decimal to Binary in C#

Explanation:

A decimal number (base 10) is converted to its binary equivalent (base 2) using the following logic: repeatedly divide the number by 2 and note the remainders. The binary digits (bits) in reverse order are represented by these remainders.

Steps for Decimal to Binary Conversion:

  1. Start with the decimal number.
    • For example: n = 13
  2. Divide the number by 2.
    • Record the remainder (it will be 0 or 1). This remainder is a binary digit.
  3. Update the number as the quotient of the division.
    • Repeat the division process until the quotient becomes 0.
  4. The binary number is the sequence of remainders read in reverse order.

Example Conversion:

Convert 13 to binary:

  1. 13 ÷ 2 = 6 remainder 1 → Binary digit: 1
  2. 6 ÷ 2 = 3 remainder 0 → Binary digit: 0
  3. 3 ÷ 2 = 1 remainder 1 → Binary digit: 1
  4. 1 ÷ 2 = 0 remainder 1 → Binary digit: 1

Binary equivalent (reversed remainders): 1101

Program to Convert Decimal to Binary

#include <stdio.h>
void decimalToBinary(int n) {
    int binaryNum[32];
    int i = 0;
    while (n > 0) {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    for (int j = i - 1; j >= 0; j--)
        printf("%d", binaryNum[j]);
}
int main() {
    int n;
    printf("Enter a decimal number: ");
    scanf("%d", &n);
    printf("Binary: ");
    decimalToBinary(n);
    return 0;
}

#include <iostream>
using namespace std;

void decimalToBinary(int n) {
    string binary = "";
    while (n > 0) {
        binary = (n % 2 == 0 ? "0" : "1") + binary;
        n /= 2;
    }
    cout << binary;
}

int main() {
    int n;
    cout << "Enter a decimal number: ";
    cin >> n;
    cout << "Binary: ";
    decimalToBinary(n);
    return 0;
}

def decimal_to_binary(n):
    return bin(n).replace("0b", "")

n = int(input("Enter a decimal number: "))
print("Binary:", decimal_to_binary(n))

<?php
function decimalToBinary($n) {
    echo decbin($n);
}
$n = (int)readline("Enter a decimal number: ");
echo "Binary: ";
decimalToBinary($n);
?>

import java.util.Scanner;

public class DecimalToBinary {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        int n = sc.nextInt();
        System.out.println("Binary: " + Integer.toBinaryString(n));
    }
}

function decimalToBinary(n) {
    return n.toString(2);
}

const n = parseInt(prompt("Enter a decimal number: "));
console.log("Binary:", decimalToBinary(n));

using System;

class DecimalToBinary {
    static void Main() {
        Console.Write("Enter a decimal number: ");
        int n = int.Parse(Console.ReadLine());
        Console.WriteLine("Binary: " + Convert.ToString(n, 2));
    }
}

List of All Programs