Program to print binary equivalent of a given decimal integer

Below is the program in C Language that will take decimal number as input. It will then call a function that will calculate the binary equivalent of the input decimal number.

#include
#include

int i = 0;
int b[10];

void main()
{
    int dec;
    int* DecimalToBinary(int n);
    int *p, j;
    
    printf("Enter a decimal integer = ");
    scanf("%d", &dec);
    
    p = DecimalToBinary(dec);
    
    printf("Binary of given decimal integer = ");
    for (j=i; j>=0; j--)
    	printf("%d", p[j]);
    
    printf("\n");
    
    getch();
}
    
int* DecimalToBinary(int n)
    {
    while (n != 1)
    {
        b[i] = n % 2;
        n = n / 2;
        i++;
    }
    
    if (n == 1)
    {
    	b[i] = 1;
    }
    else
    {
    	b[i] = 0;
    }
    
    return b;
}

You may also like...

7 Responses

  1. Anonymous says:

    Superb!!!

  2. Anonymous says:

    #include
    int main()
    {
    int n,i,m,j,rem[100],bin[100];
    printf(“\n Enter the number to be converted : “);
    scanf(“%d”,&n);
    i=0;
    m=n;
    while(n!=0)
    {
    rem[i]=n%2;
    n=n/2;
    i++;
    }
    for(j=0;j {
    bin[j]=rem[i-1-j];
    }
    printf(“\n Binary Equivalent of %d is : “,m);
    printf(“\n”);
    for(j=0;j printf(“%d”,bin[j]);
    printf(“\n\n”);
    return 0;
    }

  3. Very good solution.plz add more programs

  1. March 2, 2014

    […] Program to print binary equivalent of a given decimal integer […]

Leave a Reply

Your email address will not be published. Required fields are marked *