Convert decimal to binary number
Converting a decimal number to a binary number means representing a base 10 (decimal) integer in binary, which is a base 2 number system. In binary, there are only two digits available: 0 and 1. Each digit in a binary number represents a power of 2, starting from the rightmost digit representing 2^0 (which is 1), then the next digit representing 2^1 (which is 2), and so on. The decimal number is divided by 2 repeatedly until it reaches 0, and the remainders of each division give the binary digits in reverse order. The binary digits are then reversed to obtain the final binary representation of the decimal number. For example, the decimal number 13 can be represented as 1101 in binary.
Program Solution
// C Program
// Convert decimal to binary number
#include <stdio.h>
//Display binary value of given decimal number
void decimal_to_binary(int number)
{
//flag which is used to print the binary result
int flag = 0;
printf("Decimal : %d ", number);
printf("\nBinary : ");
if (number < 0)
{
printf(" -");
number = -number;
}
//compare value from left to right
for (int bits = 31; bits >= 0; bits--)
{
if (((number >> bits) & 1) == 0b1)
{
printf(" 1 ");
flag = 1;
}
else if (flag == 1)
{
printf(" 0 ");
}
}
printf("\n\n");
}
int main()
{
//Test Cases
decimal_to_binary(15);
decimal_to_binary(-17);
decimal_to_binary(10);
decimal_to_binary(39);
decimal_to_binary(17);
return 0;
}
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
/*
C++ Program
Convert decimal to binary number
*/
#include<iostream>
using namespace std;
class MyNumber
{
public:
//Display binary value of given decimal number
void decimal_to_binary(int number)
{
//flag which is used to print the binary result
int flag = 0;
cout << "Decimal : " << number << " ";
cout << "\nBinary : ";
if (number < 0)
{
cout << " -";
number = -number;
}
//compare value from left to right
for (int bits = 31; bits >= 0; bits--)
{
if (((number >> bits) & 1) == 0b1)
{
cout << " 1 ";
flag = 1;
}
else if (flag == 1)
{
cout << " 0 ";
}
}
cout << "\n\n";
}
};
int main()
{
MyNumber obj = MyNumber();
//Test Cases
obj.decimal_to_binary(15);
obj.decimal_to_binary(-17);
obj.decimal_to_binary(10);
obj.decimal_to_binary(39);
obj.decimal_to_binary(17);
return 0;
}
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
/*
Java Program
Convert decimal to binary number
*/
public class MyNumber
{
//Display binary value of given decimal number
public void decimal_to_binary(int number)
{
//flag which is used to print the binary result
int flag = 0;
System.out.print("Decimal : " + number + " ");
System.out.print("\nBinary : ");
if (number < 0)
{
System.out.print(" -");
number = -number;
}
//compare value from left to right
for (int bits = 31; bits >= 0; bits--)
{
if (((number >> bits) & 1) == 0b1)
{
System.out.print(" 1 ");
flag = 1;
}
else if (flag == 1)
{
System.out.print(" 0 ");
}
}
System.out.print("\n\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.decimal_to_binary(15);
obj.decimal_to_binary(-17);
obj.decimal_to_binary(10);
obj.decimal_to_binary(39);
obj.decimal_to_binary(17);
}
}
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
/*
C# Program
Convert decimal to binary number
*/
using System;
public class MyNumber
{
//Display binary value of given decimal number
public void decimal_to_binary(int number)
{
//flag which is used to print the binary result
int flag = 0;
Console.Write("Decimal : " + number + " ");
Console.Write("\nBinary : ");
if (number < 0)
{
Console.Write(" -");
number = -number;
}
//compare value from left to right
for (int bits = 31; bits >= 0; bits--)
{
if (((number >> bits) & 1) == 0b1)
{
Console.Write(" 1 ");
flag = 1;
}
else if (flag == 1)
{
Console.Write(" 0 ");
}
}
Console.Write("\n\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.decimal_to_binary(15);
obj.decimal_to_binary(-17);
obj.decimal_to_binary(10);
obj.decimal_to_binary(39);
obj.decimal_to_binary(17);
}
}
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
<?php
/*
Php Program
Convert decimal to binary number
*/
class MyNumber
{
//Display binary value of given decimal number
public function decimal_to_binary($number)
{
//flag which is used to print the binary result
$flag = 0;
echo("Decimal : ". $number ." ");
echo("\nBinary : ");
if ($number < 0)
{
echo(" -");
$number = -$number;
}
//compare value from left to right
for ($bits = 31; $bits >= 0; $bits--)
{
if ((($number >> $bits) & 1) == 0b1)
{
echo(" 1 ");
$flag = 1;
}
else if ($flag == 1)
{
echo(" 0 ");
}
}
echo("\n\n");
}
}
function main()
{
$obj = new MyNumber();
//Test Cases
$obj->decimal_to_binary(15);
$obj->decimal_to_binary(-17);
$obj->decimal_to_binary(10);
$obj->decimal_to_binary(39);
$obj->decimal_to_binary(17);
}
main();
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
/*
Node Js Program
Convert decimal to binary number
*/
class MyNumber
{
//Display binary value of given decimal number
decimal_to_binary(number)
{
//flag which is used to print the binary result
var flag = 0;
process.stdout.write("Decimal : " + number + " ");
process.stdout.write("\nBinary : ");
if (number < 0)
{
process.stdout.write(" -");
number = -number;
}
//compare value from left to right
for (var bits = 31; bits >= 0; bits--)
{
if (((number >> bits) & 1) == 0b1)
{
process.stdout.write(" 1 ");
flag = 1;
}
else
if (flag == 1)
{
process.stdout.write(" 0 ");
}
}
process.stdout.write("\n\n");
}
}
function main(args)
{
var obj = new MyNumber();
//Test Cases
obj.decimal_to_binary(15);
obj.decimal_to_binary(-17);
obj.decimal_to_binary(10);
obj.decimal_to_binary(39);
obj.decimal_to_binary(17);
}
main();
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
# Python 3 Program
# Convert decimal to binary number
class MyNumber :
# Display binary value of given decimal number
def decimal_to_binary(self, number) :
# flag which is used to print the binary result
flag = 0
print("Decimal : ", number ," ", end = "")
print("\nBinary : ", end = "")
if (number < 0) :
print(" -", end = "")
number = -number
bits = 31
# compare value from left to right
while (bits >= 0) :
if (((number >> bits) & 1) == 0b1) :
print(" 1 ", end = "")
flag = 1
elif (flag == 1) :
print(" 0 ", end = "")
bits -= 1
print("\n\n", end = "")
def main() :
obj = MyNumber()
# Test Cases
obj.decimal_to_binary(15)
obj.decimal_to_binary(-17)
obj.decimal_to_binary(10)
obj.decimal_to_binary(39)
obj.decimal_to_binary(17)
if __name__ == "__main__": main()
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
# Ruby Program
# Convert decimal to binary number
class MyNumber
# Display binary value of given decimal number
def decimal_to_binary(number)
# flag which is used to print the binary result
flag = 0
print("Decimal : ", number ," ")
print("\nBinary : ")
if (number < 0)
print(" -")
number = -number
end
bits = 31
# compare value from left to right
while (bits >= 0)
if (((number >> bits) & 1) == 0b1)
print(" 1 ")
flag = 1
elsif (flag == 1)
print(" 0 ")
end
bits -= 1
end
print("\n\n")
end
end
def main()
obj = MyNumber.new()
# Test Cases
obj.decimal_to_binary(15)
obj.decimal_to_binary(-17)
obj.decimal_to_binary(10)
obj.decimal_to_binary(39)
obj.decimal_to_binary(17)
end
main()
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
/*
Scala Program
Convert decimal to binary number
*/
class MyNumber
{
//Display binary value of given decimal number
def decimal_to_binary(num: Int): Unit = {
//flag which is used to print the binary result
var flag: Int = 0;
var number: Int = num;
print("Decimal : " + number + " ");
print("\nBinary : ");
if (number < 0)
{
print(" -");
number = -number;
}
var bits: Int = 31;
//compare value from left to right
while (bits >= 0)
{
if (((number >> bits) & 1) == 1)
{
print(" 1 ");
flag = 1;
}
else if (flag == 1)
{
print(" 0 ");
}
bits -= 1;
}
print("\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Cases
obj.decimal_to_binary(15);
obj.decimal_to_binary(-17);
obj.decimal_to_binary(10);
obj.decimal_to_binary(39);
obj.decimal_to_binary(17);
}
}
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
/*
Swift Program
Convert decimal to binary number
*/
class MyNumber
{
//Display binary value of given decimal number
func decimal_to_binary(_ num: Int)
{
//flag which is used to print the binary result
var flag: Int = 0;
var number: Int = num;
print("Decimal : ", number ," ", terminator: "");
print("\nBinary : ", terminator: "");
if (number < 0)
{
print(" -", terminator: "");
number = -number;
}
var bits: Int = 31;
//compare value from left to right
while (bits >= 0)
{
if (((number >> bits) & 1) == 0b1)
{
print(" 1 ", terminator: "");
flag = 1;
}
else
if (flag == 1)
{
print(" 0 ", terminator: "");
}
bits -= 1;
}
print("\n\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Cases
obj.decimal_to_binary(15);
obj.decimal_to_binary(-17);
obj.decimal_to_binary(10);
obj.decimal_to_binary(39);
obj.decimal_to_binary(17);
}
main();
Output
Decimal : 15
Binary : 1 1 1 1
Decimal : -17
Binary : - 1 0 0 0 1
Decimal : 10
Binary : 1 0 1 0
Decimal : 39
Binary : 1 0 0 1 1 1
Decimal : 17
Binary : 1 0 0 0 1
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