Convert a number from decimal to binary in C, C++, Java and Python | faceprep

Program to convert a number from decimal to binary is discussed here.

For example, the binary equivalent of decimal number 15 is 1111. The decimal number 15 is recursively divided by 2 until it cannot be divided any further. In each of these cases, the remainder is to be noted. The reverse of the remainder gives the binary equivalent of the decimal number. Let us now look at an example to understand this better.

Considering the same example,

15 / 2 = 7, rem = 1
7 / 2 = 3 , rem = 1
3 / 2 = 1 , rem = 1
1 / 2 = 0 , rem = 1

Binary equivalent of 15 is 1111. 

decimal to binary

Algorithm

  • Input the decimal number.
  • Divide the number by 2 and store the remainder.
  • Repeat step 2 until the number cannot be divided any further
  • Print the remainder in a reversed order.

Program to convert a number from decimal to binary

// C program to convert a number from decimal to binary

#include <stdio.h>
#include <math.h>

long int decimal_to_binary(int n)
{
long int binary = 0;
int remainder, i = 1, flag = 1;

while (n!=0)
{
remainder = n%2;
n /= 2;
binary += remainder*i;
i *= 10;
}
return binary;
}

int main()
{
int n;
printf(“\nEnter a decimal number: “);
scanf(“%d”, &n);
printf(“\nEquivalent binary number : %d\n”, decimal_to_binary(n));
return 0;
}

// C++ program to convert a number from decimal to binary

#include <iostream>
#include <math.h>
using namespace std;

long int decimal_to_binary(int n)
{
long int binary = 0;
int remainder, i = 1, flag = 1;

while (n!=0)
{
remainder = n%2;
n /= 2;
binary += remainder*i;
i *= 10;
}
return binary;
}

int main()
{
int n;
cout << “\nEnter a decimal number: “;
cin >> n;
cout << “\nEquivalent binary number : ” << decimal_to_binary(n) << endl;
return 0;
}

// Java program to convert a number from decimal to binary

import java.util.*;
public class decimal_to_binary
{
public static int decimal_to_binary(int n)
{
int binary = 0;
int remainder, i = 1, flag = 1;

while (n!=0)
{
remainder = n%2;
n /= 2;
binary += remainder*i;
i *= 10;
}
return binary;
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the decimal number : “);
int n = sc.nextInt();
System.out.println(“Equivalent binary number : ” + decimal_to_binary(n));
}
}

# Python program to convert a number from decimal to binary

import math
def decimal_to_binary(n):
binary = 0
i = 1
flag = 1
while (n!=0):
remainder = n%2
n = int(n/2)
binary = binary + remainder*i
i = i * 10
return binary

n = int(input(“Enter the binary number : “))
print(“Equivalent binary number : “,decimal_to_binary(n))

The given number N, a positive integer, will be passed to the program using the first command line parameter. Print the equivalent binary number to stdout. Other than the binary number, no other extra information should be printed to stdout Example: Given input “19”, here N=19, expected output 10011

Solution:

#include<stdio.h>
#include<math.h>
int main(int a, char *argv[])
{
int number, count, i;
int b[32];
number = atoi(argv[1]);
count = 0;
while(number != 0)
{
b[count]=number%2;
number = number/2;
count++;
}
for(i=(count-1); i>=0; i–)
printf(“%d”, b[i]);
return 0;
}

Output:

decimal to binary

Time complexity: O(n)