Program for Decimal to Binary Conversion
Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent binary number.
Examples:
Input : 7 Output : 111 Input : 10 Output : 1010 Input: 33 Output: 100001
Algorithm:
- Store the remainder when the number is divided by 2 in an array.
- Divide the number by 2
- Repeat the above two steps until the number is greater than zero.
- Print the array in reverse order now.
For Example:
If the binary number is 10.
Step 1: Remainder when 10 is divided by 2 is zero. Therefore, arr[0] = 0.
Step 2: Divide 10 by 2. New number is 10/2 = 5.
Step 3: Remainder when 5 is divided by 2 is 1. Therefore, arr[1] = 1.
Step 4: Divide 5 by 2. New number is 5/2 = 2.
Step 5: Remainder when 2 is divided by 2 is zero. Therefore, arr[2] = 0.
Step 6: Divide 2 by 2. New number is 2/2 = 1.
Step 7: Remainder when 1 is divided by 2 is 1. Therefore, arr[3] = 1.
Step 8: Divide 1 by 2. New number is 1/2 = 0.
Step 9: Since number becomes = 0. Print the array in reverse order. Therefore the equivalent binary number is 1010.
Below diagram shows an example of converting the decimal number 17 to equivalent binary number.
Below is the implementation of above idea.
C/C++
// C++ program to convert a decimal // number to binary number #include <iostream> using namespace std; // function to convert decimal to binary void decToBinary( int n) { // array to store binary number int binaryNum[32]; // counter for binary array int i = 0; while (n > 0) { // storing remainder in binary array binaryNum[i] = n % 2; n = n / 2; i++; } // printing binary array in reverse order for ( int j = i - 1; j >= 0; j--) cout << binaryNum[j]; } // Driver program to test above function int main() { int n = 17; decToBinary(n); return 0; } |
Java
// Java program to convert a decimal // number to binary number import java.io.*; class GFG { // function to convert decimal to binary static void decToBinary( int n) { // array to store binary number int [] binaryNum = new int [ 32 ]; // counter for binary array int i = 0 ; while (n > 0 ) { // storing remainder in binary array binaryNum[i] = n % 2 ; n = n / 2 ; i++; } // printing binary array in reverse order for ( int j = i - 1 ; j >= 0 ; j--) System.out.print(binaryNum[j]); } // driver program public static void main(String[] args) { int n = 17 ; decToBinary(n); } } // Contributed by Pramod Kumar |
Python3
# Python3 program to convert a # decimal number to binary number # function to convert # decimal to binary def decToBinary(n): # array to store # binary number binaryNum = [ 0 ] * n; # counter for binary array i = 0 ; while (n > 0 ): # storing remainder # in binary array binaryNum[i] = n % 2 ; n = int (n / 2 ); i + = 1 ; # printing binary array # in reverse order for j in range (i - 1 , - 1 , - 1 ): print (binaryNum[j], end = ""); # Driver Code n = 17 ; decToBinary(n); # This code is contributed by mits |
C#
// C# program to convert a decimal // number to binary number using System; public class GFG { // function to convert decimal // to binary static void decToBinary( int n) { // array to store binary number int [] binaryNum = new int [32]; // counter for binary array int i = 0; while (n > 0) { // storing remainder in // binary array binaryNum[i] = n % 2; n = n / 2; i++; } // printing binary array // in reverse order for ( int j = i - 1; j >= 0; j--) Console.Write(binaryNum[j]); } // Driver Code public static void Main() { int n = 17; decToBinary(n); } } // This code is contributed by Sam007. |
PHP
<?php // PHP program to convert a decimal // number to binary number // function to convert // decimal to binary function decToBinary( $n ) { // array to store // binary number $binaryNum ; // counter for binary array $i = 0; while ( $n > 0) { // storing remainder // in binary array $binaryNum [ $i ] = $n % 2; $n = (int)( $n / 2); $i ++; } // printing binary array // in reverse order for ( $j = $i - 1; $j >= 0; $j --) echo $binaryNum [ $j ]; } // Driver Code $n = 17; decToBinary( $n ); // This code is contributed by m_kit ?> |
Output :
10001
We can use bitwise operators to do above job. Note that bitwise operators work faster than arithmetic operators used above.
C++
// CPP program to Decimal to binary conversion // using bitwise operator // Size of an integer is assumed to be 32 bits #include <iostream> using namespace std; // Function that convert Decimal to binary int decToBinary( int n) { // Size of an integer is assumed to be 32 bits for ( int i = 31; i >= 0; i--) { int k = n >> i; if (k & 1) cout << "1" ; else cout << "0" ; } } // driver code int main() { int n = 32; decToBinary(n); } |
Java
// Java program to Decimal to binary conversion // using bitwise operator // Size of an integer is assumed to be 32 bits class gfg { // Function that convert Decimal to binary public void decToBinary( int n) { // Size of an integer is assumed to be 32 bits for ( int i = 31 ; i >= 0 ; i--) { int k = n >> i; if ((k & 1 ) > 0 ) System.out.print( "1" ); else System.out.print( "0" ); } } } class geek { // driver code public static void main(String[] args) { gfg g = new gfg(); int n = 32 ; g.decToBinary(n); } } // This code is contributed by mits |
Python3
# Python3 program to Decimal # to binary conversion using # bitwise operator # Size of an integer is # assumed to be 32 bits # Function that convert # Decimal to binary def decToBinary(n): # Size of an integer is # assumed to be 32 bits for i in range ( 31 , - 1 , - 1 ): k = n >> i; if (k & 1 ): print ( "1" , end = ""); else : print ( "0" , end = ""); # Driver Code n = 32 ; decToBinary(n); # This code is contributed by mits |
C#
// C# program to Decimal to binary conversion // using bitwise operator // Size of an integer is assumed to be 32 bits using System; class gfg { // Function that convert Decimal to binary public void decToBinary( int n) { // Size of an integer is assumed to be 32 bits for ( int i = 31; i >= 0; i--) { int k = n >> i; if ((k & 1) > 0) Console.Write( "1" ); else Console.Write( "0" ); } } } class geek { // driver code public static int Main() { gfg g = new gfg(); int n = 32; g.decToBinary(n); return 0; } } |
PHP
<?php // PHP program to Decimal to binary conversion // using bitwise operator // Size of an integer is assumed // to be 32 bits // Function that convert Decimal to binary function decToBinary( $n ) { // Size of an integer is // assumed to be 32 bits for ( $i = 31; $i >= 0; $i --) { $k = $n >> $i ; if ( $k & 1) echo "1" ; else echo "0" ; } } // Driver Code $n = 32; decToBinary( $n ); // This code is contributed by aj_36 ?> |
Output :
00000000000000000000000000100000
Thanks to ajay0007 for suggesting above solution.
Decimal to binary conversion can also be done without using arrays.
C++
// C++ implementation of the approach #include <cmath> #include <iostream> using namespace std; #define ull unsigned long long int // Function to return the binary // equivalent of decimal value N int decimalToBinary( int N) { // To store the binary number ull B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; ull c = pow (10, cnt); B_Number += rem * c; N /= 2; // Count used to store exponent value cnt++; } return B_Number; } // Driver code int main() { int N = 17; cout << decimalToBinary(N); return 0; } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to return the binary // equivalent of decimal value N static int decimalToBinary( int N) { // To store the binary number int B_Number = 0 ; int cnt = 0 ; while (N != 0 ) { int rem = N % 2 ; double c = Math.pow( 10 , cnt); B_Number += rem * c; N /= 2 ; // Count used to store exponent value cnt++; } return B_Number; } // Driver code public static void main (String[] args) { int N = 17 ; System.out.println (decimalToBinary(N)); } } // This code is contribted by ajit. |
C#
// C# implementation of the approach using System; class GFG { // Function to return the binary // equivalent of decimal value N static int decimalToBinary( int N) { // To store the binary number int B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; int c = ( int )Math.Pow(10, cnt); B_Number += rem * c; N /= 2; // Count used to store exponent value cnt++; } return B_Number; } // Driver code static public void Main () { int N = 17; Console.Write(decimalToBinary(N)); } } // This code is contributed by Tushil. |
Output :
10001
Note that this method is similar to the one where we convert Binary to Decimal as discussed in this post.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Program for Binary To Decimal Conversion
- Decimal to binary conversion without using arithmetic operators
- Program for decimal to hexadecimal conversion
- Program for Decimal to Octal Conversion
- Program for Octal to Decimal Conversion
- Recursive Program for Binary to Decimal
- Python program to convert decimal to binary number
- 8086 program to convert a 16 bit decimal number to binary
- Decimal to octal conversion with minimum use of arithmetic operators
- Program for Celsius To Fahrenheit conversion
- Decimal to binary number using recursion
- Convert Binary fraction to Decimal
- Decimal to Binary using recursion and without using power operator
- Decimal representation of given binary string is divisible by 10 or not
- Decimal representation of given binary string is divisible by 5 or not