Quantcast
Channel: C Programming Archives - QnA Plus
Viewing all articles
Browse latest Browse all 93

C Program to Convert Decimal to Binary

$
0
0

Following C programs converts a decimal number to its binary equivalent.

Using recursive function

#include <stdio.h>

void binary(int n)
{
    if(n > 0)
    {
        binary(n/2);
        printf("%d", n%2);
    }
}

void main()
{
    int num = 0;

    printf("Enter an integer: ");
    scanf("%d", &num);

    printf("Binary of %d: ", num);
    binary(num);
    printf("\n");
}

Without recursive function

#include <stdio.h>

void main()
{
    int num = 0, i, tmp;

    printf("Enter an integer: ");
    scanf("%d", &num);

    printf("Binary of %d: ", num);

    i = sizeof(num) * 8 - 1;

    while(i >= 0) printf("%d", (num >> i--)&1?1:0);

    printf("\n");
}

Output:

Enter an integer: 534
Binary of 534: 1000010110

The post C Program to Convert Decimal to Binary appeared first on QnA Plus.


Viewing all articles
Browse latest Browse all 93

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>