Solved programming problem

Convert Decimal to Binary with Recursion in C.

Source code

#include<stdio.h>

int binary(int n)

{

    if(n==0) ///base case

    {

        return 0;

    }

    else

    {

        return (n%2)+10 * binary(n/2); ///calling the same function till n!=0

    }

}

int main()

{

    int n;

    printf("Please Enter a Number: ");

    scanf("%d",&n);

    int ans = binary(n); ///calling the function  and also save the return in ans integer

    printf("Binary: %d",ans);

}

Output

Please Enter a Number: 10
Binary: 1010

Join the discussion

Add your comment