Decimal to binary conversion without using arithmetic operators
Find the binary equivalent of the given non-negative number n without using arithmetic operators.
Examples:
Input : n = 10
Output : 1010Input : n = 38
Output : 100110
Note that + in below algorithm/program is used for concatenation purpose.
Algorithm:
decToBin(n)
if n == 0
return "0"
Declare bin = ""
Declare ch
while n > 0
if (n & 1) == 0
ch = '0'
else
ch = '1'
bin = ch + bin
n = n >> 1
return bin
Below is the implementation of above approach:
C++
// C++ implementation of decimal to binary conversion // without using arithmetic operators #include <bits/stdc++.h> using namespace std; // function for decimal to binary conversion // without using arithmetic operators string decToBin( int n) { if (n == 0) return "0" ; // to store the binary equivalent of decimal string bin = "" ; while (n > 0) { // to get the last binary digit of the number 'n' // and accumulate it at the beginning of 'bin' bin = ((n & 1) == 0 ? '0' : '1' ) + bin; // right shift 'n' by 1 n >>= 1; } // required binary number return bin; } // Driver program to test above int main() { int n = 38; cout << decToBin(n); return 0; } |
Java
// Java implementation of decimal // to binary conversion without // using arithmetic operators import java.io.*; class GFG { // function for decimal to // binary conversion without // using arithmetic operators static String decToBin( int n) { if (n == 0 ) return "0" ; // to store the binary // equivalent of decimal String bin = "" ; while (n > 0 ) { // to get the last binary digit // of the number 'n' and accumulate // it at the beginning of 'bin' bin = ((n & 1 ) == 0 ? '0' : '1' ) + bin; // right shift 'n' by 1 n >>= 1 ; } // required binary number return bin; } // Driver program to test above public static void main (String[] args) { int n = 38 ; System.out.println(decToBin(n)); } } // This code is contributed by vt_m |
Python3
# Python3 implementation of # decimal to binary conversion # without using arithmetic operators # function for decimal to # binary conversion without # using arithmetic operators def decToBin(n): if (n = = 0 ): return "0" ; # to store the binary # equivalent of decimal bin = ""; while (n > 0 ): # to get the last binary # digit of the number 'n' # and accumulate it at # the beginning of 'bin' if (n & 1 = = 0 ): bin = '0' + bin ; else : bin = '1' + bin ; # right shift 'n' by 1 # It also gives n//2 . n = n >> 1 ; # required binary number return bin ; # Driver Code n = 38 ; print (decToBin(n)); # This code is contributed # by mits |
C#
// C# implementation of decimal // to binary conversion without // using arithmetic operators using System; class GFG { // function for decimal to // binary conversion without // using arithmetic operators static String decToBin( int n) { if (n == 0) return "0" ; // to store the binary // equivalent of decimal String bin = "" ; while (n > 0) { // to get the last binary digit // of the number 'n' and accumulate // it at the beginning of 'bin' bin = ((n & 1) == 0 ? '0' : '1' ) + bin; // right shift 'n' by 1 n >>= 1; } // required binary number return bin; } // Driver program to test above public static void Main() { int n = 38; Console.WriteLine(decToBin(n)); } } // This code is contributed by Sam007 |
Javascript
<script> // javascript implementation of decimal // to binary conversion without // using arithmetic operators // function for decimal to // binary conversion without // using arithmetic operators function decToBin(n) { if (n == 0) return "0" ; // to store the binary // equivalent of decimal var bin = "" ; while (n > 0) { // to get the last binary digit // of the number 'n' and accumulate // it at the beginning of 'bin' bin = ((n & 1) == 0 ? '0' : '1' ) + bin; // right shift 'n' by 1 n >>= 1; } // required binary number return bin; } // Driver program to test above var n = 38; document.write(decToBin(n)); // This code is contributed by shikhasingrajput </script> |
PHP
<?php // PHP implementation of decimal // to binary conversion without // using arithmetic operators // function for decimal to // binary conversion without // using arithmetic operators function decToBin( $n ) { if ( $n == 0) return "0" ; // to store the binary // equivalent of decimal $bin = "" ; while ( $n > 0) { // to get the last binary // digit of the number 'n' // and accumulate it at // the beginning of 'bin' $bin = (( $n & 1) == 0 ? '0' : '1' ) . $bin ; // right shift 'n' by 1 $n >>= 1; } // required binary number return $bin ; } // Driver Code $n = 38; echo decToBin( $n ); // This code is contributed // by mits ?> |
Output:
100110
Time complexity: O(num), where num is the number of bits in the binary representation of n.
Auxiliary space: O(num), for using extra space for string bin.
METHOD 2:Using format()
APPROACH:
This code converts a decimal number to binary using the built-in format() function in Python. The function takes two arguments: the first is the number to be converted, and the second is the format specifier ‘b’, which tells the function to convert the number to binary.
ALGORITHM:
1. Take the decimal number as input.
2. Convert the decimal number to binary using the format() function with the format specifier ‘b’.
3. Store the result in a variable.
4. Print the variable.
C++
// CPP code of the above approach #include <bits/stdc++.h> using namespace std; int main() { int n = 38; // Convert n to binary representation as a string string binary = bitset<32>(n).to_string(); cout << "The binary representation of " << n << " is: " << stoi(binary) << endl; n = 10; // Convert n to binary representation as a string binary = bitset<32>(n).to_string(); cout << "The binary representation of " << n << " is: " << stoi(binary) << endl; return 0; } // This code is contributed by Susobhan Akhuli |
Java
// Java code of the above approach import java.util.Scanner; public class GFG { public static void main(String[] args) { int n = 38 ; // Convert n to binary representation as a string String binary = Integer.toBinaryString(n); System.out.println( "The binary representation of " + n + " is: " + binary); n = 10 ; // Convert n to binary representation as a string binary = Integer.toBinaryString(n); System.out.println( "The binary representation of " + n + " is: " + binary); } } // This code is contributed by Susobhan Akhuli |
Python3
n = 38 binary = format (n, 'b' ) print (f "The binary representation of {n} is: {binary}" ) n = 10 binary = format (n, 'b' ) print (f "The binary representation of {n} is: {binary}" ) |
C#
// C# code of the above approach using System; public class GFG { static void Main() { int n = 38; // Convert n to binary representation as a string string binary = Convert.ToString(n, 2); Console.WriteLine( $ "The binary representation of {n} is: {binary}" ); n = 10; // Convert n to binary representation as a string binary = Convert.ToString(n, 2); Console.WriteLine( $ "The binary representation of {n} is: {binary}" ); } } // This code is contributed by Susobhan Akhuli |
Javascript
// JavaScript program for the above approach function decimalToBinary(n) { // Convert n to binary representation as a string let binary = n.toString(2); return binary; } // Test cases let n1 = 38; console.log(`The binary representation of ${n1} is: ${decimalToBinary(n1)}`); let n2 = 10; console.log(`The binary representation of ${n2} is: ${decimalToBinary(n2)}`); // This code is contributed by Susobhan Akhuli |
The binary representation of 38 is: 100110 The binary representation of 10 is: 1010
Time complexity: O(log n), where n is the decimal number, because the number of iterations required in the format() function depends on the number of bits required to represent the number in binary, which is log2(n).
Space complexity: O(log n), because the space required to store the binary representation of the number in the variable also depends on the number of bits required to represent the number in binary, which is log2(n).