C program to convert Decimal to Binary number system

Previous Program Next Program

Write a C program to input decimal number from user and convert to binary number system. How to convert from decimal number to binary number system in C. Algorithm to convert decimal to binary number system in C program.

Example

Input

Input decimal number: 112

Output

Binary number: 0111000

Required knowledge

Basic C programming, For loop

Decimal number system

Decimal number system is a base 10 number system. Decimal number system uses only 10 symbols to represent all number i.e. 0123456789

Binary number system

Binary number system is a base 2 number system. Binary number system uses only 2 symbols to represent all numbers i.e. 0 and1

Algorithm to convert from decimal to binary

Algorithm Decimal to Binary conversion
begin:
read (decimal);
binary ← 0; place ← 1; rem ← 0;
while (decimal > 0) do 
begin 
    remdecimal % 2;
    binary ← (rem * place) + binary;
    placeplace * 10;
    decimaldecimal / 2;
end
write('Binary = ' binary)
end

Program to convert decimal to binary

/**
 * C program to convert from Decimal to Binary number system
 */

#include <stdio.h>

int main()
{
    long long decimal, tempDecimal, binary;
    int rem, place = 1;

    binary = 0;

    /*
     * Input decimal number from user
     */
    printf("Enter any decimal number: ");
    scanf("%lld", &decimal);
    tempDecimal = decimal;

    /*
     * Decimal to binary conversion
     */
    while(tempDecimal > 0)
    {
        rem = tempDecimal % 2;

        binary = (rem * place) + binary;

        tempDecimal /= 2;

        place *= 10;
    }

    printf("Decimal number = %lld\n", decimal);
    printf("Binary number = %lld", binary);

    return 0;
}

Note: The above program converts the decimal to binary number only up to 18 binary bits. Use the below program to convert from decimal to binary number for a higher range.

Program to convert decimal to binary more than 18 bits

/**
 * C program to convert from Decimal to Binary number system
 */

#include <stdio.h>
#include <string.h>

int main()
{
    long decimal, tempDecimal;
    char binary[65];
    int index = 0;
    
    /*
     * Input decimal number from user
     */
    printf("Enter any decimal value : ");
    scanf("%ld", &decimal);
    
    /* Copy decimal value to temp variable */
    tempDecimal = decimal;
    
    while(tempDecimal > 0)
    {
        binary[index] = (tempDecimal % 2) + '0';
        
        tempDecimal /= 2;

        index++;
    }
    binary[index] = '\0';
    
    /* Reverse the converted binary to get final binary result */
    strrev(binary);

    printf("Decimal value = %ld\n", decimal);
    printf("Binary value of decimal = %s", binary);

    return 0;
}
Output
Enter any decimal value : 123
Decimal value = 123
Binary value of decimal = 1101111

Happy coding ;)

Recommended posts

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

2 comments:

  1. go recursive ...

    /* decimal to binary */
    void decimal_to_binary(int a)
    {
    int lsb;
    if(a == 0)
    return;
    lsb = a & 1;
    decimal_to_binary(a>>1);
    printf("%d", lsb);
    }

    ReplyDelete
    Replies
    1. Thanks @meh for the short recursive approach. I will update it the with same.

      Delete