Decimal to Binary using recursion and without using power operator - GeeksforGeeks

04 Aug, 2021
Given an integer N, the task is convert and print the binary equaiva;ent of N.Examples:  
Input: N = 13 Output: 1101Input: N = 15 Output: 1111  
 
Approach Write a recursive function that takes an argument N and recursively calls itself with the value N / 2 as the new argument and prints N % 2 after the call. The base condition will be when N = 0, simply print 0 and return out of the function in that case.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Recursive function to convert n// to its binary equivalentvoid decimalToBinary(int n){    // Base case    if (n == 0) {        cout << "0";        return;    }     // Recursive call    decimalToBinary(n / 2);    cout << n % 2;} // Driver codeint main(){    int n = 13;     decimalToBinary(n);     return 0;}
// Java implementation of the approachimport java.io.*; class GFG{      // Recursive function to convert n// to its binary equivalentstatic void decimalToBinary(int n){    // Base case    if (n == 0)    {        System.out.print("0");        return;    }     // Recursive call    decimalToBinary(n / 2);    System.out.print( n % 2);} // Driver codepublic static void main (String[] args){    int n = 13;     decimalToBinary(n);}} // This code is contributed by anuj_67..
# Python3 implementation of the approach # Recursive function to convert n# to its binary equivalentdef decimalToBinary(n) :         # Base case    if (n == 0) :        print("0",end="");        return;         # Recursive call    decimalToBinary(n // 2);    print(n % 2,end="");  # Driver codeif __name__ == "__main__" :     n = 13;    decimalToBinary(n);     # This code is contributed by AnkitRai01
// C# implementation of the approachusing System;     class GFG{         // Recursive function to convert n    // to its binary equivalent    static void decimalToBinary(int n)    {                 // Base case        if (n == 0)        {            Console.Write("0");            return;        }         // Recursive call        decimalToBinary(n / 2);        Console.Write(n % 2);    }     // Driver code    public static void Main(String[] args)    {        int n = 13;         decimalToBinary(n);    }} // This code is contributed by 29AjayKumar
<script>// javascript implementation of the approach     // Recursive function to convert n    // to its binary equivalent    function decimalToBinary(n) {        // Base case        if (n == 0) {            document.write("0");            return;        }         // Recursive call        decimalToBinary(parseInt(n / 2));        document.write(n % 2);    }     // Driver code             var n = 13;         decimalToBinary(n); // This code contributed by gauravrajput1</script>
01101
 
Time Complexity: O(logN)Auxiliary Space: O(logN)
ankthon
vt_m
29AjayKumar
GauravRajput1
pankajsharmagfg
Algorithms
Mathematical
Recursion
Mathematical
Recursion
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
Difference between Informed and Uninformed Search in AI
How to write a Pseudo Code?
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7