Convert Binary to Decimal number

Convert binary number into decimal number

Here given code implementation process.

// C Program
// Convert Binary to Decimal number
#include <stdio.h>

void binary_to_decimal(long long number)
{
	// Used to store result
	long long result = 0;
	int counter = 0;
	// Display Binary number
	printf("Binary : %lld ", number);
	while (number != 0)
	{
		if ((number & 1) == 0b1)
		{
			// When get 1 in form of binary bit
			result += (number % 10) *(1 << (counter));
		}
		counter++;
		number /= 10;
	}
	// Display decimal result
	printf(" Decimal : %lld \n", result);
}
int main()
{
	// Pass a decimal number which is contain binary values
	binary_to_decimal(1111);
	binary_to_decimal(10111);
	binary_to_decimal(10101);
	binary_to_decimal(1001);
	binary_to_decimal(1101);
	return 0;
}

Output

Binary : 1111  Decimal : 15
Binary : 10111  Decimal : 23
Binary : 10101  Decimal : 21
Binary : 1001  Decimal : 9
Binary : 1101  Decimal : 13
/*
  Java Program
  Convert Binary to Decimal number
*/
public class DecimalNumber
{
  public void binaryTodecimal(long number)
  {
    // Used to store result
    long result = 0;
    int counter = 0;
    // Display Binary number
    System.out.print("Binary :  " + number);
    // Execute loop when number is greater than zero
    while (number != 0)
    {
      if ((number & 1) == 0b1)
      {
        // When get 1 in form of binary bit
        result += (number % 10) * (1 << (counter));
      }
      counter++;
      number /= 10;
    }
    // Display decimal result
    System.out.println("  Decimal :  " + result );
  }
  public static void main(String[] args)
  {
    DecimalNumber task = new DecimalNumber();
    // Test Cases
    // Pass a decimal number which is contain binary values
    task.binaryTodecimal(1111);
    task.binaryTodecimal(10111);
    task.binaryTodecimal(10101);
    task.binaryTodecimal(1001);
    task.binaryTodecimal(1101);
  }
}

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
// Include header file
#include <iostream>
using namespace std;
/*
  C++ Program
  Convert Binary to Decimal number
*/
class DecimalNumber
{
	public: void binaryTodecimal(long number)
	{
		// Used to store result
		long result = 0;
		int counter = 0;
		// Display Binary number
		cout << "Binary :  " << number;
		// Execute loop when number is greater than zero
		while (number != 0)
		{
			if ((number & 1) == 1)
			{
				// When get 1 in form of binary bit
				result += (number % 10) *(1 << (counter));
			}
			counter++;
			number /= 10;
		}
		// Display decimal result
		cout << "  Decimal :  " << result << endl;
	}
};
int main()
{
	DecimalNumber *task = new DecimalNumber();
	// Test Cases
	// Pass a decimal number which is contain binary values
	task->binaryTodecimal(1111);
	task->binaryTodecimal(10111);
	task->binaryTodecimal(10101);
	task->binaryTodecimal(1001);
	task->binaryTodecimal(1101);
	return 0;
}

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
package main
import "fmt"
/*
  Go Program
  Convert Binary to Decimal number
*/
type DecimalNumber struct {}
func getDecimalNumber() * DecimalNumber {
	var me *DecimalNumber = &DecimalNumber {}
	return me
}
func(this DecimalNumber) binaryTodecimal(number int64) {
	// Used to store result
	var result int64 = 0
	var counter int = 0
	// Display Binary number
	fmt.Print("Binary :  ", number)
	// Execute loop when number is greater than zero
	for (number != 0) {
		if (number & 1) == 1 {
			// When get 1 in form of binary bit
			result += (number % 10) * (1 << (counter))
		}
		counter++
		number /= 10
	}
	// Display decimal result
	fmt.Println("  Decimal :  ", result)
}
func main() {
	var task * DecimalNumber = getDecimalNumber()
	// Test Cases
	// Pass a decimal number which is contain binary values
	task.binaryTodecimal(1111)
	task.binaryTodecimal(10111)
	task.binaryTodecimal(10101)
	task.binaryTodecimal(1001)
	task.binaryTodecimal(1101)
}

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
// Include namespace system
using System;
/*
  Csharp Program
  Convert Binary to Decimal number
*/
public class DecimalNumber
{
	public void binaryTodecimal(long number)
	{
		// Used to store result
		long result = 0;
		int counter = 0;
		// Display Binary number
		Console.Write("Binary :  " + number);
		// Execute loop when number is greater than zero
		while (number != 0)
		{
			if ((number & 1) == 1)
			{
				// When get 1 in form of binary bit
				result += (number % 10) * (1 << (counter));
			}
			counter++;
			number /= 10;
		}
		// Display decimal result
		Console.WriteLine("  Decimal :  " + result);
	}
	public static void Main(String[] args)
	{
		DecimalNumber task = new DecimalNumber();
		// Test Cases
		// Pass a decimal number which is contain binary values
		task.binaryTodecimal(1111);
		task.binaryTodecimal(10111);
		task.binaryTodecimal(10101);
		task.binaryTodecimal(1001);
		task.binaryTodecimal(1101);
	}
}

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
<?php
/*
  Php Program
  Convert Binary to Decimal number
*/
class DecimalNumber
{
	public	function binaryTodecimal($number)
	{
		// Used to store result
		$result = 0;
		$counter = 0;
		// Display Binary number
		echo("Binary :  ".$number);
		// Execute loop when number is greater than zero
		while ($number != 0)
		{
			if (($number & 1) == 1)
			{
				// When get 1 in form of binary bit
				$result += ($number % 10) * (1 << ($counter));
			}
			$counter++;
			$number = $number / 10;
		}
		// Display decimal result
		echo("  Decimal :  ".$result."\n");
	}
}

function main()
{
	$task = new DecimalNumber();
	// Test Cases
	// Pass a decimal number which is contain binary values
	$task->binaryTodecimal(1111);
	$task->binaryTodecimal(10111);
	$task->binaryTodecimal(10101);
	$task->binaryTodecimal(1001);
	$task->binaryTodecimal(1101);
}
main();

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
/*
  Node JS Program
  Convert Binary to Decimal number
*/
class DecimalNumber
{
	binaryTodecimal(number)
	{
		// Used to store result
		var result = 0;
		var counter = 0;
		// Display Binary number
		process.stdout.write("Binary :  " + number);
		// Execute loop when number is greater than zero
		while (number != 0)
		{
			if ((number & 1) == 1)
			{
				// When get 1 in form of binary bit
				result += (number % 10) * (1 << (counter));
			}
			counter++;
			number = parseInt(number / 10);
		}
		// Display decimal result
		console.log("  Decimal :  " + result);
	}
}

function main()
{
	var task = new DecimalNumber();
	// Test Cases
	// Pass a decimal number which is contain binary values
	task.binaryTodecimal(1111);
	task.binaryTodecimal(10111);
	task.binaryTodecimal(10101);
	task.binaryTodecimal(1001);
	task.binaryTodecimal(1101);
}
main();

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
#  Python 3 Program
#  Convert Binary to Decimal number
class DecimalNumber :
	def binaryTodecimal(self, number) :
		#  Used to store result
		result = 0
		counter = 0
		#  Display Binary number
		print("Binary :  ", number, end = "")
		#  Execute loop when number is greater than zero
		while (number != 0) :
			if ((number & 1) == 1) :
				#  When get 1 in form of binary bit
				result += (number % 10) * (1 << (counter))
			
			counter += 1
			number = number // 10
		
		#  Display decimal result
		print("  Decimal :  ", result)
	

def main() :
	task = DecimalNumber()
	#  Test Cases
	#  Pass a decimal number which is contain binary values
	task.binaryTodecimal(1111)
	task.binaryTodecimal(10111)
	task.binaryTodecimal(10101)
	task.binaryTodecimal(1001)
	task.binaryTodecimal(1101)

if __name__ == "__main__": main()

Output

Binary :   1111  Decimal :   15
Binary :   10111  Decimal :   23
Binary :   10101  Decimal :   21
Binary :   1001  Decimal :   9
Binary :   1101  Decimal :   13
#  Ruby Program
#  Convert Binary to Decimal number
class DecimalNumber 
	def binaryTodecimal(number) 
		#  Used to store result
		result = 0
		counter = 0
		#  Display Binary number
		print("Binary :  ", number)
		#  Execute loop when number is greater than zero
		while (number != 0) 
			if ((number & 1) == 1) 
				#  When get 1 in form of binary bit
				result += (number % 10) * (1 << (counter))
			end

			counter += 1
			number = number / 10
		end

		#  Display decimal result
		print("  Decimal :  ", result, "\n")
	end

end

def main() 
	task = DecimalNumber.new()
	#  Test Cases
	#  Pass a decimal number which is contain binary values
	task.binaryTodecimal(1111)
	task.binaryTodecimal(10111)
	task.binaryTodecimal(10101)
	task.binaryTodecimal(1001)
	task.binaryTodecimal(1101)
end

main()

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
/*
  Scala Program
  Convert Binary to Decimal number
*/
class DecimalNumber()
{
	def binaryTodecimal(num: Long): Unit = {
		// Used to store result
		var result: Long = 0;
		var counter: Int = 0;
		// Display Binary number
		print("Binary :  " + num);
  		var number = num;
		// Execute loop when number is greater than zero
		while (number != 0)
		{
			if ((number & 1) == 1)
			{
				// When get 1 in form of binary bit
				result += (number % 10) * (1 << (counter));
			}
			counter += 1;
			number = number / 10;
		}
		// Display decimal result
		println("  Decimal :  " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DecimalNumber = new DecimalNumber();
		// Test Cases
		// Pass a decimal number which is contain binary values
		task.binaryTodecimal(1111);
		task.binaryTodecimal(10111);
		task.binaryTodecimal(10101);
		task.binaryTodecimal(1001);
		task.binaryTodecimal(1101);
	}
}

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13
/*
  Swift 4 Program
  Convert Binary to Decimal number
*/
class DecimalNumber
{
	func binaryTodecimal(_ num: Int)
	{
		// Used to store result
		var result: Int = 0;
		var counter: Int = 0;
      	var number: Int = num;
		// Display Binary number
		print("Binary :  ", number, terminator: "");
		// Execute loop when number is greater than zero
		while (number  != 0)
		{
			if ((number & 1) == 1)
			{
				// When get 1 in form of binary bit
				result += (number % 10) * (1 << (counter));
			}
			counter += 1;
			number = number / 10;
		}
		// Display decimal result
		print("  Decimal :  ", result);
	}
}
func main()
{
	let task: DecimalNumber = DecimalNumber();
	// Test Cases
	// Pass a decimal number which is contain binary values
	task.binaryTodecimal(1111);
	task.binaryTodecimal(10111);
	task.binaryTodecimal(10101);
	task.binaryTodecimal(1001);
	task.binaryTodecimal(1101);
}
main();

Output

Binary :   1111  Decimal :   15
Binary :   10111  Decimal :   23
Binary :   10101  Decimal :   21
Binary :   1001  Decimal :   9
Binary :   1101  Decimal :   13
/*
  Kotlin Program
  Convert Binary to Decimal number
*/
class DecimalNumber
{
	fun binaryTodecimal(num: Long): Unit
	{
		// Used to store result
		var result: Long = 0;
		var counter: Int = 0;
      	var number: Long = num;
		// Display Binary number
		print("Binary :  " + number);
		// Execute loop when number is greater than zero
		while (number != 0L)
		{
			if ((number and 1) == 1L)
			{
				// When get 1 in form of binary bit
				result += (number % 10) * (1 shl(counter));
			}
			counter += 1;
			number = number / 10;
		}
		// Display decimal result
		println("  Decimal :  " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: DecimalNumber = DecimalNumber();
	// Test Cases
	// Pass a decimal number which is contain binary values
	task.binaryTodecimal(1111);
	task.binaryTodecimal(10111);
	task.binaryTodecimal(10101);
	task.binaryTodecimal(1001);
	task.binaryTodecimal(1101);
}

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved