入力として 10 進数を指定すると、指定された 10 進数を同等の 2 進数に変換するプログラムを作成する必要があります。
個別の SQL カウント
の例 10 進数から 2 進数へ :
Input : 7 Output : 111 Input : 10 Output : 1010 Input: 33 Output: 100001>推奨される実践 10 進数から 2 進数へ試してみましょう!
ブルートフォースアプローチ
例えば :
10進数が10の場合。
ステップ1 :10を2で割った余りが0になります。したがって、arr[0] = 0となります。
ステップ2 : 10 を 2 で割ります。新しい数値は 10/2 = 5 です。
ステップ3 : 5を2で割った余りは1です。よって、arr[1] = 1となります。
ステップ4 : 5 を 2 で割ります。新しい数は 5/2 = 2 です。
ステップ5 :2を2で割った余りが0になります。したがって、arr[2] = 0となります。
ステップ6 : 2 を 2 で割ります。新しい数値は 2/2 = 1 です。
ステップ7 : 1を2で割った余りは1です。よって、arr[3] = 1となります。
ステップ8 : 1 を 2 で割ります。新しい数値は 1/2 = 0 です。
ステップ9 : 数値が = 0 になるので、配列を逆順に出力します。したがって、同等の 2 進数は 1010 となります。
以下の図は、10 進数の 17 を同等の 2 進数に変換する例を示しています。

以下は上記のアイデアを実装したものです。
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) { // 剰余をバイナリ配列に格納 binaryNum[i] = n % 2; n = n / 2; i++; } // バイナリ配列を逆順に出力 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; }> 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) { // 剰余をバイナリ配列に格納 binaryNum[i] = n % 2; n = n / 2; i++; } // バイナリ配列を逆順で出力します for (int j = i - 1; j>= 0; j--) printf('%d', binaryNum[j]); } // 上記をテストするドライバープログラム function int main() { int n = 17; decToBinary(n); 0を返します。 }>> ジャワ // 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) { // 剰余をバイナリ配列に格納 binaryNum[i] = n % 2; n = n / 2; i++; } // バイナリ配列を逆順に出力します for (int j = i - 1; j>= 0; j--) System.out.print(binaryNum[j]); } // ドライバープログラム public static void main(String[] args) { int n = 17; decToBinary(n); } } // 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) { // 剰余をバイナリ配列に格納 // binaryNum[i] = n % 2; n = n / 2; i++; } // バイナリ配列を逆順で出力します for (int j = i - 1; j>= 0; j--) Console.Write(binaryNum[j]); } // ドライバーコード public static void Main() { int n = 17; decToBinary(n); } } // このコードは 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) { // 剰余をバイナリ配列に格納 // $binaryNum[$i] = $n % 2; $n = (int)($n / 2); $i++; } // バイナリ配列を逆順で出力 for ($j = $i - 1; $j>= 0; $j--) echo $binaryNum[$j]; } // ドライバー コード $n = 17; decToBinary($n); // このコードは 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): # 剰余をバイナリ配列に格納 binaryNum[i] = n % 2 n = int(n / 2) i += 1 # バイナリ配列を出力 # range(i - 1, -1, -1): print(binaryNum[j], end = '') # ドライバー コード n = 17 decToBinary(n) # このコードは mits によって提供されています>>
出力
10001>
時間計算量: O(ログン) & 補助スペース: ○(1)
ビット演算子を使用して上記の作業を行うことができます。 ビット演算子は、上で使用した算術演算子よりも高速に動作することに注意してください。
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; i--) { int k = n>> i; if (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; i--) { int k = n>> i; // 右シフト if (k & 1) // 最初のビットの状態を知るのに役立ちます printf('1'); それ以外の場合は printf('0'); } } // ドライバーコード int main() { int n = 32; decToBinary(n); }>> ジャワ // 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 { // ドライバー コード public static void main(String[] args) { gfg g = new gfg(); int n = 32; g.decToBinary(n); } } // このコードは 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 { // ドライバーコード public static int Main() { gfg g = new gfg(); int n = 32; g.decToBinary(n); 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; if ($k & 1) echo '1';それ以外の場合は '0' をエコーします。 } } // ドライバーコード $n = 32; decToBinary($n); // このコードは 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='') # ドライバー コード n = 32 decToBinary(n ) # このコードは mits によって提供されました>> 出力
00000000000000000000000000100000>
時間計算量: ○(1)
ループは小さい数であっても毎回定数(32)回繰り返します
補助スペース: ○(1)
効率的なアプローチ
これは、右シフト (>>) 演算子と And (&) 演算子を使用して 10 進数を 2 進数に変換するもう 1 つの効率的なアプローチです。ここでは、通常、計算が非常に高速な二項演算子のみを使用します。
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; // 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 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'; else // 0 str += '0'; 数値>>= 1; // 1 だけ右シフト } return str; } // 文字列を反転します 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('数値 59 のバイナリは: '); reverse(DecimalToBinary(num)); } } // このコードは phasing17 によって提供されています>>'C#0) { if ((num & 1) == 1) // 1 str += '1'; else // 0 str += '0'; 数値>>= 1; // 1 だけ右シフト } return str; } // 文字列を反転します public static void reverse(String str) { for (int i = str.Length - 1; i>= 0; i--) Console.Write(str[i]); } // ドライバーコード public static void Main(string[] args) { int num = 59; Console.Write('数値 59 のバイナリは: '); reverse(DecimalToBinary(num)); } } // このコードは phasing17 によって提供されました>>' JavaScript>= 1; 文字列を返します。 } // str の逆を出力する関数 function reverse(str) { var res = ''; for (var i = str.length - 1; i>= 0; i--) res += (str[i]); document.write(res); } // ドライバーコード var num = 59; document.write('num ' + num + ' のバイナリは: '); // 関数呼び出し reverse(DecimalToBinary(num)); // このコードは phasing17 によって提供されています>> 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 # 文字列を反転する関数 def reverse(strs): print(strs[::-1]) # ドライバー コード num = 59 print('num 59 のバイナリは:', end=' ') reverse(DecimalToBinary(num)) # このコードは phasing17 によって提供されています>>'
出力 Binary of num 59 is: 111011>
時間計算量: O(log n) & 補助スペース: ○(1)
10 進数から 2 進数への変換は、配列を使用せずに実行することもできます。
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 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>
出力
任意の 10 進数を 2 進数形式に変換するさらに別の方法があります。アイデアは、使用することです ビットセット 。
以下は、上記のアプローチの実装です。
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(); //最初に出現した '1' を検索し、 //先頭のゼロを削除します。 const auto loc1 = s.find('1'); if(loc1 != string::npos) return s.substr(loc1); '0' を返します; } //ドライバーコード int main() { int n = 17; //関数呼び出しcout<< decimalToBinary(n); return 0; } //This code is contributed by yashbeersingh42> ジャワ // 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))> 出力
10001>
時間計算量: O(ログン) & 補助スペース: ○(1)
別のアプローチ
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>(数値).to_string().substr(64 - n - 1); } // このコードは phasing17 によって書かれています>>' // 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> 出力
the binary number is : 1111>