This is a tool to practice converting between decimal and binary representations. After you have practiced for a while and feel that you know how to do the.
web developer and programmer tools
World's simplest decimal converter. Just paste your decimal integer in the form below, press Convert button, and you get a binary number in base 2. Press button, get binary. No ads, nonsense or garbage.
Announcement: We just added another two new tools categories –
PNG Tools and UTF8 Tools. Check them out!
PNG Tools and UTF8 Tools. Check them out!
(undo)
Want to convert binary to decimal?
Use the Bin to dec converter!
Looking for more programming tools? Try these!
Pro tip: You can use ?input=text query argument to pass text to tools.
Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent binary number.
Examples:
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
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.
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.
Below is the implementation of above idea.
C/C++
// number to binary number #include <iostream> void decToBinary( int n) // array to store binary number int i = 0; binaryNum[i] = n % 2; i++; for ( int j = i - 1; j >= 0; j--) } // Driver program to test above function { decToBinary(n); } |
Java
// number to binary number // function to convert decimal to binary { int [] binaryNum = new int [ 32 ]; // counter for binary array while (n > 0 ) { binaryNum[i] = n % 2 ; i++; for ( int j = i - 1 ; j >= 0 ; j--) } // driver program { decToBinary(n); } // Contributed by Pramod Kumar |
Python3
# decimal number to binary number # function to convert def decToBinary(n): # array to store binaryNum = [ 0 ] * n; # counter for binary array while (n > 0 ): # storing remainder binaryNum[i] = n % 2 ; i + = 1 ; # printing binary array for j in range (i - 1 , - 1 , - 1 ): n = 17 ; |
C#
// number to binary number // to binary { int [] binaryNum = new int [32]; // counter for binary array while (n > 0) { // binary array n = n / 2; } // printing binary array for ( int j = i - 1; j >= 0; j--) } // Driver Code { decToBinary(n); } // This code is contributed by Sam007. |
PHP
// PHP program to convert a decimal // decimal to binary { // binary number $i = 0; { // storing remainder $binaryNum [ $i ] = $n % 2; $i ++; // in reverse order echo $binaryNum [ $j ]; $n = 17; ?> |
Output :
We can use bitwise operators to do above job. Note that bitwise operators work faster than arithmetic operators used above.
C++
// using bitwise operator #include <iostream> int decToBinary( int n) // Size of an integer is assumed to be 32 bits int k = n >> i; cout << '1' ; cout << '0' ; } // driver code { decToBinary(n); |
Java
// using bitwise operator // Function that convert Decimal to binary { for ( int i = 31 ; i >= 0 ; i--) { if ((k & 1 ) > 0 ) else } } class geek { public static void main(String[] args) gfg g = new gfg(); g.decToBinary(n); } |
Python3
# to binary conversion using # assumed to be 32 bits # Function that convert def decToBinary(n): # Size of an integer is for i in range ( 31 , - 1 , - 1 ): if (k & 1 ): else : n = 32 ; |
C#
// using bitwise operator using System; // Function that convert Decimal to binary { for ( int i = 31; i >= 0; i--) { if ((k & 1) > 0) else } } class geek { public static int Main() gfg g = new gfg(); g.decToBinary(n); } |
PHP
// PHP program to Decimal to binary conversion // to be 32 bits // Function that convert Decimal to binary { // Size of an integer is for ( $i = 31; $i >= 0; $i --) $k = $n >> $i ; echo '1' ; echo '0' ; } // Driver Code decToBinary( $n ); // This code is contributed by aj_36 |
Output :
Thanks to ajay0007 for suggesting above solution.
Decimal to binary conversion can also be done without using arrays.
C++
#include <cmath> using namespace std; #define ull unsigned long long int // Function to return the binary int decimalToBinary( int N) ull B_Number = 0; while (N != 0) { ull c = pow (10, cnt); N /= 2; // Count used to store exponent value } return B_Number; int main() } |
Java
import java.io.*; class GFG // equivalent of decimal value N { // To store the binary number int cnt = 0 ; { double c = Math.pow( 10 , cnt); N /= 2 ; // Count used to store exponent value } return B_Number; public static void main (String[] args) System.out.println (decimalToBinary(N)); } |
C#
using System; class GFG // equivalent of decimal value N { // To store the binary number int cnt = 0; { int c = ( int )Math.Pow(10, cnt); N /= 2; // Count used to store exponent value } return B_Number; static public void Main () int N = 17; } |
Output :
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 [email protected]. 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:
Improved By : Sam007, jit_t, Mithun Kumar, SoM15242, sirraghavgupta, more