Decimal to Binary using recursion and without using power operator

Given an integer N, the task is convert and print the binary equaiva;ent of N.

Examples:

Input: N = 13
Output: 1101

Input: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Recursive function to convert n
// to its binary equivalent
void decimalToBinary(int n)
{
    // Base case
    if (n == 0) {
        cout << "0";
        return;
    }
  
    // Recursive call
    decimalToBinary(n / 2);
    cout << n % 2;
}
  
// Driver code
int main()
{
    int n = 13;
  
    decimalToBinary(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG
{
      
  
// Recursive function to convert n
// to its binary equivalent
static 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 code
public static void main (String[] args) 
{
    int n = 13;
  
    decimalToBinary(n);
}
}
  
// This code is contributed by anuj_67..

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Recursive function to convert n 
# to its binary equivalent 
def decimalToBinary(n) :
      
    # Base case 
    if (n == 0) :
        print("0",end=""); 
        return;
      
    # Recursive call 
    decimalToBinary(n // 2); 
    print(n % 2,end=""); 
  
  
# Driver code 
if __name__ == "__main__"
  
    n = 13;
    decimalToBinary(n); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

// C# implementation of the approach
using 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

Output:

01101


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, vt_m, 29AjayKumar