Gegeven een decimaal getal als invoer, moeten we een programma schrijven om het gegeven decimale getal om te zetten in een equivalent binair getal.
json-bestanden lezen
Voorbeelden van Decimaal naar binair :
Input : 7 Output : 111 Input : 10 Output : 1010 Input: 33 Output: 100001>Aanbevolen praktijkDecimaal naar binairProbeer het!
Brute kracht aanpak
Bijvoorbeeld :
Als het decimale getal 10 is.
Stap 1 : De rest als 10 wordt gedeeld door 2 is nul. Daarom arr[0] = 0.
Stap 2 : Deel 10 door 2. Nieuw getal is 10/2 = 5.
Stap 3 : De rest als 5 wordt gedeeld door 2 is 1. Daarom is arr[1] = 1.
Stap 4 : Deel 5 door 2. Nieuw getal is 5/2 = 2.
Stap 5 : De rest als 2 gedeeld wordt door 2 is nul. Daarom arr[2] = 0.
Stap 6 : Deel 2 door 2. Nieuw getal is 2/2 = 1.
Stap 7 : De rest als 1 wordt gedeeld door 2 is 1. Daarom is arr[3] = 1.
Stap 8 : Deel 1 door 2. Nieuw getal is 1/2 = 0.
Stap 9 : Omdat getal = 0 wordt. Druk de array in omgekeerde volgorde af. Daarom is het equivalente binaire getal 1010.
Het onderstaande diagram toont een voorbeeld van het converteren van het decimale getal 17 naar een equivalent binair getal.

Hieronder vindt u de implementatie van het bovenstaande idee.
C++ // C++ program to convert a decimal // number to binary number #include 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) {// rest opslaan in binaire array binaryNum[i] = n % 2; n = n/2; ik++; } // binaire array in omgekeerde volgorde afdrukken voor (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; }> C // C Code to convert Decimal number into Binary #include void decToBinary(int n) { // array to store binary number int binaryNum[32]; // counter for binary array int i = 0; while (n>0) {// rest opslaan in binaire array binaryNum[i] = n % 2; n = n/2; ik++; } // binaire array in omgekeerde volgorde afdrukken voor (int j = i - 1; j>= 0; j--) printf('%d', binaryNum[j]); } // Stuurprogramma om bovenstaande functie te testen int main() { int n = 17; decToBinair(n); retour 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) {// rest opslaan in binaire array binaryNum[i] = n % 2; n = n/2; ik++; } // binaire array in omgekeerde volgorde afdrukken voor (int j = i - 1; j>= 0; j--) System.out.print(binaryNum[j]); } // stuurprogramma public static void main(String[] args) { int n = 17; decToBinair(n); } } // Bijgedragen door Pramod Kumar> 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) {// rest opslaan in // binaire array binaryNum[i] = n % 2; n = n/2; ik++; } // binaire array afdrukken // in omgekeerde volgorde voor (int j = i - 1; j>= 0; j--) Console.Write(binaryNum[j]); } // Stuurprogrammacode public static void Main() { int n = 17; decToBinair(n); } } // Deze code is bijgedragen door Sam007.> Javascript >
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) {/ // restant opslaan // in binaire array $binaryNum[$i] = $n % 2; $n = (int)($n/2); $i++; } // binaire array afdrukken // in omgekeerde volgorde voor ($j = $i - 1; $j>= 0; $j--) echo $binaryNum[$j]; } // Stuurprogrammacode $n = 17; decToBinary($n); // Deze code is bijgedragen door m_kit ?>> 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): # opslag van de rest # in binaire array binaryNum[i] = n % 2 n = int(n / 2) i += 1 # afdrukken van binaire array # in omgekeerde volgorde voor j in bereik(i - 1, -1, -1): print(binaryNum[j], end = '') # Stuurprogrammacode n = 17 decToBinary(n) # Deze code is bijgedragen door mits>
Uitvoer
10001>
Tijdcomplexiteit: O(login) & Hulpruimte: O(1)
We kunnen bitsgewijze operatoren gebruiken om het bovenstaande werk te doen. Merk op dat bitsgewijze operatoren sneller werken dan de hierboven gebruikte rekenkundige operatoren.
C++ // CPP program to Decimal to binary conversion // using bitwise operator // Size of an integer is assumed to be 32 bits #include using namespace std; // Function that convert Decimal to binary void decToBinary(int n) { // Size of an integer is assumed to be 32 bits for (int i = 31; i>= 0; ik--) { int k = n>> ik; als (k & 1) cout<< '1'; else cout << '0'; } } // driver code int main() { int n = 32; decToBinary(n); }> C // C language to convert Decimal to binary number // using bitwise operator // Size of an integer is assumed to be 32 bits #include // 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; ik--) { int k = n>> ik; // shift naar rechts if (k & 1) // helpt ons de status van het eerste bit printf('1') te kennen; anders printf('0'); } } // stuurprogrammacode int main() { int n = 32; decToBinair(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; ik--) { int k = n>> ik; als ((k & 1)> 0) Systeem.uit.print('1'); anders Systeem.uit.print('0'); } } } class geek { // drivercode public static void main(String[] args) { gfg g = new gfg(); intn = 32; g.decToBinary(n); } } // Deze code is bijgedragen door 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; ik--) { int k = n>> ik; als ((k & 1)> 0) Console.Write('1'); anders Console.Write('0'); } } } class geek { // stuurprogrammacode public static int Main() { gfg g = new gfg(); intn = 32; g.decToBinary(n); retour 0; } }> Javascript >
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; als ($k & 1) echo '1'; anders echo '0'; } } // Stuurprogrammacode $n = 32; decToBinary($n); // Deze code is bijgedragen door aj_36 ?>> 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='') # Stuurprogrammacode n = 32 decToBinary(n ) # Deze code is bijgedragen door mits> Uitvoer
00000000000000000000000000100000>
Tijdcomplexiteit: O(1)
lus itereert een constant (32) aantal keren elke keer, zelfs voor een klein aantal
Hulpruimte: O(1)
Efficiënte aanpak
Het is een andere efficiënte benadering om decimaal naar binair te converteren met behulp van de juiste shift(>>) en And(&) operator. Hier gebruiken we alleen binaire operators die doorgaans erg snel zijn in hun berekeningen.
C++ #include using namespace std; string DecimalToBinary(int num) { string str; while(num){ if(num & 1) // 1 str+='1'; else // 0 str+='0'; num>>=1; // Rechts verschuiven met 1 } return str; } void reverse(string str) { for(int i=str.size()-1; i>=0; i--) cout<< str[i]; } int main() { int num = 59; cout<< 'Binary of num 59 is: '; reverse( DecimalToBinary(num) ); return 0; }> Java // Java program to implement the // above approach import java.io.*; class GFG { // the converts decimal to binary base static String DecimalToBinary(int num) { String str = ''; while (num>0) { if ((num & 1) == 1) // 1 str += '1'; anders // 0 str += '0'; aantal>>= 1; // Rechts verschuiven met 1 } return str; } // keer de string om static void reverse(String str) { for (int i = str.length() - 1; i>= 0; i--) System.out.print(str.charAt(i)); } public static void main(String[] args) { int num = 59; System.out.print('Binair getal van getal 59 is: '); reverse(DecimalToBinary(num)); } } // Deze code is bijgedragen door fasering17> C# // C# program to implement the // above approach using System; public class GFG { // this converts decimal to binary base public static string DecimalToBinary(int num) { string str = ''; while (num>0) { if ((num & 1) == 1) // 1 str += '1'; anders // 0 str += '0'; aantal>>= 1; // Rechts verschuiven met 1 } return str; } // keer de string om public static void reverse(String str) { for (int i = str.Length - 1; i>= 0; i--) Console.Write(str[i]); } // Stuurprogrammacode public static void Main(string[] args) { int num = 59; Console.Write('Binair getal van getal 59 is: '); reverse(DecimalToBinary(num)); } } // deze code is bijgedragen door fasering17> Javascript >
Python3 # Python3 program to implement the above approach # function to convert the decimal number # to binary number def DecimalToBinary(num): strs = '' while num: # if (num & 1) = 1 if (num & 1): strs += '1' # if (num & 1) = 0 else: strs += '0' # right shift by 1 num>>= 1 return strs # functie om de string om te keren def reverse(strs): print(strs[::-1]) # Stuurprogrammacode num = 59 print('Binair van num 59 is:', end=' ') reverse(DecimalToBinary(num)) # Deze code is bijgedragen door phasing17> Uitvoer
Binary of num 59 is: 111011>
Tijdcomplexiteit: O(logboek n) & Hulpruimte: O(1)
Decimaal naar binair conversie kan ook worden gedaan zonder gebruik te maken van arrays.
C++ // C++ implementation of the approach #include #include 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; } // This code is contributed by Sania Kumari Gupta (kriSania804)> C // C implementation of the approach #include #include #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; printf('%u', decimalToBinary(N)); return 0; } // This code is contributed by Sania Kumari Gupta (kriSania804)> 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 contributed 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.> Javascript >
Python3 # Python3 implementation of the approach # Function to return the binary # equivalent of decimal value N def decimalToBinary(N): # To store the binary number B_Number = 0 cnt = 0 while (N != 0): rem = N % 2 c = pow(10, cnt) B_Number += rem * c N //= 2 # Count used to store exponent value cnt += 1 return B_Number # Driver code N = 17 print(decimalToBinary(N)) # This code is contributed by # SHUBHAMSINGH10>
Uitvoer
10001>
Tijdcomplexiteit: O(login) & Hulpruimte: O(1)
Merk op dat deze methode vergelijkbaar is met degene waarbij we binair naar decimaal converteren, zoals hierin besproken na .
Er is nog een andere methode die elk decimaal getal naar zijn binaire vorm converteert. Het idee is om te gebruiken bitset .
Hieronder vindt u de implementatie van bovenstaande aanpak.
C++ //C++ program to convert a decimal number //to its binary form. //including header file #include using namespace std; //Function to convert a decimal number //to its binary form string decimalToBinary(int n) { //finding the binary form of the number and //converting it to string. string s = bitset<64>(n).to_string(); //Het eerste exemplaar van '1' vinden //om de voorloopnullen te verwijderen. const auto loc1 = s.find('1'); if(loc1 != string::npos) retourneert s.substr(loc1); retourneer '0'; } //Stuurprogrammacode int main() { int n = 17; //Functie oproep cout<< decimalToBinary(n); return 0; } //This code is contributed by yashbeersingh42> Java // Java program to convert a decimal number to its binary // form import java.util.*; class DecimalToBinary { // Function to convert a decimal number to its binary // form public static String decimalToBinary(int n) { // Finding the binary form of the number and // converting it to a string String s = Integer.toBinaryString(n); // Finding the first occurrence of '1' to strip off // the leading zeroes int loc1 = s.indexOf('1'); if (loc1 != -1) { return s.substring(loc1); } return '0'; } // Driver code public static void main(String[] args) { int n = 17; // Function call System.out.println(decimalToBinary(n)); } } // This code is contributed by phasing17> C# // C# program to convert a decimal number // to its binary form. using System; class HelloWorld { // Function to convert a decimal number // to its binary form public static String decimalToBinary(int n) { // finding the binary form of the number and //converting it to string. String s = Convert.ToString(n, 2); return s; } static void Main() { int n = 17; //Function call Console.WriteLine(decimalToBinary(n)); } } // The code is contributed by Nidhi goel.> Javascript // Javascript program to convert a decimal number // to its binary form. // Function to convert a decimal number // to its binary form function decimalToBinary( n) { // finding the binary form of the number and // converting it to string. const s = n.toString(2); return s; } // Driver Code let n = 17; // Function call console.log(decimalToBinary(n)); // This code is contributed by imruhrbf8.> Python3 # Python program to convert a decimal number # to its binary form. # Function to convert a decimal number # to its binary form def decimalToBinary( n): # finding the binary form of the number and # converting it to string. s = bin(n)[2:] # Finding the first occurrence of '1' # to strip off the leading zeroes. # const auto loc1 = s.find('1') loc1 = s[s.index('1'):] return loc1 return '0' # Driver Code n = 17 # Function call print(decimalToBinary(n))> Uitvoer
10001>
Tijdcomplexiteit: O(login) & Hulpruimte: O(1)
Een andere aanpak
C++ // C++ program to convert Decimal to Binary Number #include using namespace std; int main() { // input number int number = 15; int n = (int)(log2(number)); // binary output // using the inbuilt function cout << 'the binary number is : ' << bitset<64>(nummer).to_string().substr(64 - n - 1); } // Deze code is geschreven door phasing17> Java //To convert Decimal to Binary Number// import java.util.*; public class Main{ public static void main(String [] args){ //input// int number =15; //output// System.out.println('the binary number is : '+ Integer.toString(number,2)); //This code is written by ZEESHAN AHMAD// } }> C# // To convert Decimal to Binary Number// using System; class GFG{ public static void Main(){ // input// int number =15; //output// Console.WriteLine('the binary number is : '+ Convert.ToString(number, 2)); } } // This code is contributed by code_hunt.> Javascript // JavaScript program to convert Decimal to Binary Number // input number var number = 15; // binary output // using the inbuilt function console.log('the binary number is :', number.toString(2)); // This code is written by phasing17> Python3 # Python3 program to convert Decimal to Binary Number # input number number = 15 # binary output # using the inbuilt function print('the binary number is :', bin(number)[2::]) # This code is written by phasing17> Uitvoer
the binary number is : 1111>