Description
To convert binary to decimal, extract rightmost digits from binary number and multiply the digit with the proper base (Power of 2) and add it to the variable. At the end, the variable will store the required decimal number.
C/C++
/* C Program to convert Binary to Decimal */ //Save it as ConvertBinaryToDecimal.c #include<stdio.h> int main(){ int bin, rem, dec = 0, count=0; printf("Enter a binary Number : "); scanf("%d",&bin); //Storing the value of binary to temporary variable int temp = bin; while(bin > 0) { rem = bin % 10; dec += (rem * pow(2,count)); bin /= 10; count++; } //Restoring the binary variable bin = temp; printf("The decimal of %d is %d", bin, dec); return 0; }
Input: Enter a binary Number : 11010 Output: The decimal of 11010 is 26
Java
/* Java Program to convert Binary to Decimal */ //Save it as ConvertBinaryToDecimal.java import java.io.*; import java.util.Scanner; public class ConvertBinaryToDecimal { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a binary Number : "); int bin = scanner.nextInt(); //Storing the value of binary to temporary variable int temp = bin; int rem, dec = 0, count=0; while(bin > 0) { rem = bin % 10; dec += (rem * Math.pow(2,count)); bin/=10; count++; } //Restoring the binary variable bin = temp; System.out.println("The decimal of "+ bin + " is "+ dec); } }
Input: Enter a binary Number : 11010 Output: The decimal of 11010 is 26
Related Programs
1) Program to convert Binary to Hexa Decimal2) Program to convert Decimal to Octal
3) Program to convert Decimal to Binary
4) Program to convert Octal to Decimal
5) Program to find sum of digits of a number
6) Program to reverse a number
7) Program to calculate Factorial of a Number
8) Program to display multiplication table of a number
9) Program to Display Fibonacci Series
10) Program to Display Fibonacci Series using Recursion
11) Program of Simple Calculator using switch
12) Program to print all Squares of numbers from 1 to given Range
13) Program to find sum of first and last digit of a number