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.