Convert a Number Decimal System to Binary System using Recursion in C

Let’s understand how to Decimal Number to Binary Number using recursion in C Programming Language.

Convert a Number Decimal System to Binary System using Recursion in C

#include <stdio.h>

 /*Learn C Programming at CodeAtGlance.com*/

int convert(int);
int main()
{
    int dec, bin;

    printf("Enter a decimal number: ");

    scanf("%d", &dec);

    bin = convert(dec);

    printf("The binary equivalent of %d is %d.\n", dec, bin);

    return 0;
}

int convert(int dec)

{
    if (dec == 0)

    {
        return 0;
    }
    else
    {
        return (dec % 2 + 10 * convert(dec / 2));
    }
}

Leave a Reply