Write a c program to convert decimal number to binary.

Ram Pothuraju

 #include <stdio.h>

int main()


    int decimal_num, quotient, remainder, binary_num = 0, i = 1;

    // Read decimal number from user

    printf("Enter a decimal number: ");

    scanf("%d", &decimal_num);

    // Convert decimal to binary

    quotient = decimal_num;

    while (quotient != 0)


        remainder = quotient % 2;

        binary_num += remainder * i;

        i *= 10;

        quotient /= 2;


    // Print binary number

    printf("Binary number is: %d", binary_num);

    return 0;


In this program, we first read a decimal number from the user using the scanf() function. We then convert this number to binary by repeatedly dividing by 2 and keeping track of the remainders. We build up the binary number digit-by-digit, starting from the least significant bit, by multiplying each remainder by a power of 10 and adding it to the previous digits.

Finally, we print the binary number using the printf() function.

Note that this program assumes that the input decimal number is non-negative. To handle negative numbers, you may need to use two's complement representation and adjust the conversion accordingly.

Post a Comment


Post a Comment (0)