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

Program to convert a number from decimal to binary can be done using four methods.

Method 1: Using for loop
Method 2: Using while loop
Method 3: Using Stack
Method 4: Using Array

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 using for loop

#include <stdio.h>
long int decimal_to_binary(int n)
{
long int binary = 0;
int remainder, i, flag = 1;
for(i = 1; n != 0; i = i * 10)
{
remainder = n % 2;
n /= 2;
binary += remainder * i;
}
return binary;
}
int main()
{
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("Equivalent binary number: %d\n", decimal_to_binary(n));
return 0;
}
#include <iostream>
using namespace std;
long int decimal_to_binary(int n)
{
long int binary = 0;
int remainder, i, flag = 1;
for(i = 1; n != 0; i = i * 10)
{
remainder = n % 2;
n /= 2;
binary += remainder * i;
}
return binary;
}
int main()
{
int n;
cout << "Enter a decimal number: ";
cin >> n;
cout << "Equivalent binary number : " << decimal_to_binary(n);
return 0;
}
import java.util.Scanner;
public class Main
{
public static long decimal_to_binary(int n)
{
long binary = 0;
int remainder, i, flag = 1;
for(i = 1; n != 0; i = i * 10)
{
remainder = n % 2;
n /= 2;
binary += remainder * i;
}
return binary;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a decimal number:");
int n = sc.nextInt();
System.out.println("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>
int main(int argc, char *argv[])
{
int n = atoi(argv[1]);
long int binary = 0;
int remainder, i, flag = 1;
for(i = 1; n != 0; i = i * 10)
{
remainder = n % 2;
n /= 2;
binary += remainder * i;
}
printf("Equivalent binary number: %d", binary);
}

Program to convert a number from decimal to binary using while loop

#include <stdio.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 = i * 10;
}
return binary;
}
int main()
{
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("Equivalent binary number: %d\n", decimal_to_binary(n));
return 0;
}
#include <iostream>
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 = i * 10;
}
return binary;
}
int main()
{
int n;
cout << "Enter a decimal number: ";
cin >> n;
cout << "Equivalent binary number : " << decimal_to_binary(n);
return 0;
}
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))
import java.util.Scanner;
public class Main
{
public static long decimal_to_binary(int n)
{
long binary = 0;
int remainder, i = 1, flag = 1;
while(n != 0)
{
remainder = n % 2;
n /= 2;
binary += remainder * i;
i = i * 10;
}
return binary;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a decimal number:");
int n = sc.nextInt();
System.out.println("Equivalent binary number : " + decimal_to_binary(n));
}
}

Program to convert a number from decimal to binary using Stack

In this method, the decimal value is divided by 2 and the remainder is stored in a stack. Once the decimal value reaches zero, print the values stored in the stack.

#include <stdio.h>
#include <conio.h>
#define MAX 100
//declaring stack using structure
typedef struct stack
{
int data[MAX];
int top;
}stack;
//checking whether the stack is empty or not
int empty(stack *s)
{
if(s -> top == -1)
return(1);
return(0);
}
//checking whether the stack is full or not
int full(stack *s)
{
if(s -> top == MAX - 1)
return(1);
return(0);
}
//to push the remainder into the stack
void push(stack *s, int x)
{
s -> top = s -> top + 1;
s -> data[s -> top] = x;
}
//to pop the remainder out from the stack
int pop(stack *s)
{
int x;
x = s -> data[s -> top];
s -> top = s -> top - 1;
return(x);
}
int main()
{
stack s; //structure member variable
int num;
s.top = -1; //top pointer initialized with -1
printf("Enter a decimal number:");
scanf("%d", &num);
while((num != 0))
{
if(!full(&s)) //this condition executes if the function 'full' returns 0
{
push(&s, num % 2); //pushing the remainder into the stack using 'push' function
num = num / 2;
}
else
{
printf("Stack overflow"); //if stack reaches the MAX value
exit(0);
}
}
while(!empty(&s))
{
num = pop(&s); //poping the remainder out from the stack using 'pop' function
printf("%d", num);
}
}
#include <iostream>
#include <conio.h>
#define MAX 100
using namespace std;
//declaring stack using structure
typedef struct stack
{
int data[MAX];
int top;
}stack;
//checking whether the stack is empty or not
int empty(stack *s)
{
if(s -> top == -1)
return(1);
return(0);
}
//checking whether the stack is full or not
int full(stack *s)
{
if(s -> top == MAX - 1)
return(1);
return(0);
}
//to push the remainder into the stack
void push(stack *s, int x)
{
s -> top = s -> top + 1;
s -> data[s -> top] = x;
}
//to pop the remainder out from the stack
int pop(stack *s)
{
int x;
x = s -> data[s -> top];
s -> top = s -> top - 1;
return(x);
}
int main()
{
stack s; //structure member variable
int num;
s.top = -1; //top pointer initialized with -1
cout << "Enter a decimal number:";
cin >> num;
while((num != 0))
{
if(!full(&s)) //this condition executes if the function 'full' returns 0
{
push(&s, num % 2); //pushing the remainder into the stack using 'push' function
num = num / 2;
}
else
{
cout << "Stack overflow"; //if stack reaches the MAX value
exit(0);
}
}
while(!empty(&s))
{
num = pop(&s); //poping the remainder out from the stack using 'pop' function
cout << num;
}
}
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Creating Stack object. Since stack is a class in Java
Stack stk = new Stack();
System.out.println("Enter a decimal number:");
int num = sc.nextInt();
while (num != 0)
{
int d = num % 2;
stk.push(d); //pushing the remainder into the stack using the stack object
num /= 2;
}
System.out.print("Equivalent binary number: ");
while (!(stk.isEmpty() ))
{
System.out.print(stk.pop());
}
System.out.println();
}
}

Program to convert a number from decimal to binary using an Array

Divide the decimal value by 2 and store the remainder in the array. Once the decimal value reaches zero, print the array values in the reverse order.

#include <stdio.h>
#define MAX 100
int main()
{
int n, i;
int arr[MAX];
printf("Enter a decimal number: ");
scanf("%d", &n);
for(i = 0; n != 0; i++)
{
arr[i] = n % 2; //storing the remainder into an array
n = n / 2;
}
printf("Equivalent binary number: ");
for(i = i - 1; i >= 0; i--)
{
printf("%d", arr[i]); //printing the array values in the reverse order
}
}
#include <iostream>
using namespace std;
#define MAX 100
int main()
{
int n, i;
int arr[MAX];
cout << "Enter a decimal number: ";
cin >> n;
for(i = 0; n != 0; i++)
{
arr[i] = n % 2; //storing the remainder into an array
n = n / 2;
}
cout << "Equivalent binary number: ";
for(i = i - 1; i >= 0; i--)
{
cout << arr[i]; //printing the array values in the reverse order
}
}
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i;
int arr[] = new int[100];
System.out.println("Enter a decimal number: ");
int n = sc.nextInt();
for(i = 0; n != 0; i++)
{
arr[i] = n % 2; //storing the remainder into an array
n = n / 2;
}
System.out.println("Equivalent binary number: ");
for(i = i - 1; i >= 0; i--)
{
System.out.print(arr[i]); //printing the array values in the reverse order
}
}
}

Output:

decimal to binary

Time complexity: O(n)