Kotlin Program:

- To convert binary number into decimal

Example:

User Input: In Binary : 100

Output: In decimal : 4

import java.util.Scanner

fun toDecimal(n1:Int):Int {
  var num=n1
  var n:Double=0.0
  var i:Double=0.0
    while(num!=0) {
      if(num%10==0 || num%10==1) {
        n=n+Math.pow(2.0,i++)*(num%10)
      }
      else {
        println("Number is not in binary form")
        return 0
      }
      num/=10
    }
    return n.toInt()
}

fun main(args: Array<String>) {

  val userInput = Scanner(System.`in`)
  print("Enter your number : ")
  var num:Int = userInput.nextInt()

  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 :

fun toDecimal(n1:Int):Int {
  var num=n1
  var n:Double=0.0
  var i:Double=0.0
    while(num!=0) {
      if(num%10==0 || num%10==1) {
        n=n+Math.pow(2.0,i++)*(num%10)
      }
      else {
        println("Number is not in binary form")
        return 0
      }
      num/=10
    }
    return n.toInt()
}
  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.