C Program Convert Decimal Number To Binary Number Using Loop

#include <stdio.h>
#include <math.h>
long decimalToBinary(int decimalnum)
{
    long binarynum = 0;
    int rem, temp = 1;
    while (decimalnum!=0)
    {
        rem = decimalnum%2;
        decimalnum = decimalnum / 2;
        binarynum = binarynum + rem*temp;
        temp = temp * 10;
    }
    return binarynum;
}
int main()
{
    int decimalnum;
    printf("Enter a Decimal Number: ");
    scanf("%d", &decimalnum);
    printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum));
    return 0;
}

Output:

Enter a Decimal Number: 234
Equivalent Binary Number is: 11101010

Leave a Comment