Program to Convert Binary to Decimal

Program to Convert Binary to Decimal

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

Explanation:

Summing up the binary digits (0 0 or 1) multiplied by 2n, where n is the position of the digit from the right (beginning with n=0), is the logic for converting a binary number (base 2) to its decimal equivalent (base 10).

Steps for Binary to Decimal Conversion:

  1. Start with the binary number.
    • For example: 1101.
  2. Identify the position of each digit starting from the rightmost digit.
    • The rightmost digit has position n=0, the next one has n=1, and so on.
  3. Multiply each binary digit by 2n.
  4. Sum up all the values.
    • The result is the decimal equivalent.

Example Conversion:

Convert 1101 to decimal:

  1. Write the binary digits with their positions:1 × 23 + 1×22 + 0×21 + 1×20
  2. Calculate each term:(1×8) + (1×4) + (0×2) + (1×1) = 8 + 4 + 0 + 1
  3. Add the terms: 8 + 4 + 0 + 1 = 13

Decimal equivalent: 13.

Program to Convert Binary to Decimal

#include <stdio.h>
#include <math.h>
int binaryToDecimal(long long n) {
    int decimal = 0, base = 1, remainder;
    while (n > 0) {
        remainder = n % 10;
        decimal += remainder * base;
        base *= 2;
        n /= 10;
    }
    return decimal;
}
int main() {
    long long n;
    printf("Enter a binary number: ");
    scanf("%lld", &n);
    printf("Decimal: %d\n", binaryToDecimal(n));
    return 0;
}

#include <iostream>
using namespace std;

void decimalToHexadecimal(int n) {
    string hex = "";
    char hexChars[] = "0123456789ABCDEF";
    while (n > 0) {
        hex = hexChars[n % 16] + hex;
        n /= 16;
    }
    cout << hex;
}

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

def binary_to_decimal(binary):
    return int(binary, 2)

binary = input("Enter a binary number: ")
print("Decimal:", binary_to_decimal(binary))

<?php
function binaryToDecimal($binary) {
    return bindec($binary);
}

$binary = readline("Enter a binary number: ");
echo "Decimal: " . binaryToDecimal($binary) . "\n";
?>

import java.util.Scanner;

public class BinaryToDecimal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        String binary = sc.nextLine();
        int decimal = Integer.parseInt(binary, 2);
        System.out.println("Decimal: " + decimal);
    }
}

function binaryToDecimal(binary) {
    return parseInt(binary, 2);
}

const binary = prompt("Enter a binary number: ");
console.log("Decimal:", binaryToDecimal(binary));

using System;

class BinaryToDecimal {
    static void Main() {
        Console.Write("Enter a binary number: ");
        string binary = Console.ReadLine();
        int decimalValue = Convert.ToInt32(binary, 2);
        Console.WriteLine("Decimal: " + decimalValue);
    }
}

List of All Programs