Java Program:

- To convert binary number into decimal

Example:

User Input: In Binary : 100

Output: In decimal : 4

import java.util.Scanner;
public class BinaryToDecimal {

  public static int toDecimal(long num) {
    double n=0,i=0;
    while(num!=0) {
      if(num%10==0 || num%10==1) {
        n=n+Math.pow(2,i++)*(num%10);
      }
      else {
        System.out.println("Number is not in binary form");
        return 0;
      }
      num/=10;
    }
    return (int)(n);
  }

  public static void main(String []args) {
    long num;
    Scanner userInput = new Scanner(System.in);
    System.out.print("Enter your binary number : ");
    num = userInput.nextLong();

    System.out.println("Number in decimal : "+toDecimal(num));
  }
}

Output:

  Enter Your binary number : 1101
  Number in decimal : 13
Program
number

Binary Numbers : Numbers with only 0 & 1 combination

Examples like: 101, 111, 10, 11111

Main Logic :

public static int toDecimal(long num) {
  double n=0,i=0;
  while(num!=0) {
    if(num%10==0 || num%10==1) {
      n=n+Math.pow(2,i++)*(num%10);
    }
    else {
      System.out.println("Number is not in binary form");
      return 0;
    }
    num/=10;
  }
  return (int)(n);
}
  Calculation:
  Lets take binary number n=100

  = 20*0 + 21*0 + 22*1
  = 0 + 0 + 4
  = 4

------------------------------------------------------------------------
  take every single digit of your binary number from right side and
  multiply it with 2 to the power of index position 
  starting form 0 at the right side 

  Index position  2 1 0
                  | | |
  Binary Number   1 0 0 
Concept

Coming Soon !

Quick
Tutorial

Quote

Sometimes it’s the smallest decisions that can change your life forever.