C program to convert decimal to binary number

In this blog post, we learn how to write a C program to convert decimal to binary number?. We will write the C program to convert decimal to binary number. Write a C program to input the decimal number and convert it to a binary number. How to convert decimal to binary number in C programming. Logic to convert decimal to binary number in C.

Example,

Input: 5
Output: 101


Input: 9
Output: 1001

 

Logic to convert decimal to binary number:

1. Ask the user to enter the decimal number.

2. Now perform modulo division on the number by 2 and store the remainder in an array.

binaryNum[i] = num % 2;

3. Now Divide the number by 2.

num = num / 2; 

4. Repeat steps 2 and 3 until the number is greater than zero.

5. In the last we will print the array in reverse order.

 

#include <stdio.h>

#define CHAR_SIZE   8
#define ARRAY_SIZE sizeof(int)* CHAR_SIZE


int main()
{
    // counter for binary array
    int i = 0,j=0;
    //num for decimal number
    int num;
    //Array to store binary number
    int binaryNum[ARRAY_SIZE];

    printf("Enter decimal number: ");
    scanf("%d", &num);

    while (num > 0)
    {
        binaryNum[i] = num % 2;
        num = num / 2;
        i++;
    }

    // printing binary array in reverse order
    for (j = i - 1; j >= 0; j--)
    {
        printf("%d",binaryNum[j]);
    }

    return 0;
}

Output:

Enter decimal number: 10
1010

Code Analysis,

Suppose num is 10.

Step 1: Remainder when 10 is divided by 2 is zero. Therefore, binaryNum[0] = 0.
Step 2: Divide 10 by 2. New number is 10/2 = 5.
Step 3: Remainder when 5 is divided by 2 is 1. Therefore, binaryNum[1] = 1.
Step 4: Divide 5 by 2. New number is 5/2 = 2.
Step 5: Remainder when 2 is divided by 2 is zero. Therefore, binaryNum[2] = 0.
Step 6: Divide 2 by 2. New number is 2/2 = 1.
Step 7: Remainder, when 1 is divided by 2, is 1. Therefore, binaryNum[3] = 1.
Step 8: Divide 1 by 2. New number is 1/2 = 0.
Step 9: Since number becomes = 0. Print the array in reverse order. Therefore the equivalent binary number is 1010.

 

We can also convert decimal to binary without using arrays. So let’s see the code,

#include <stdio.h>

//typedef to avoid long name
typedef unsigned long long int ULLINT;

// Function to return the binary
// equivalent of decimal value num
ULLINT decimalToBinary(int num)
{
    // To store the binary number
    ULLINT binNum = 0;
    int cnt = 0;
    while (num != 0)
    {
        int rem = num % 2;
        ULLINT c = pow(10, cnt);
        binNum += rem * c;
        num /= 2;
        // Count used to store exponent value
        cnt++;
    }

    return binNum;
}

int main()
{
    //num for decimal number
    int num;

    printf("Enter decimal number: ");
    scanf("%d", &num);

    //Called function
    printf("%d",decimalToBinary(num));

    return 0;
}

Output:

Enter decimal number: 67
1000011

 

We can use bitwise operators to convert decimal numbers into binary numbers.

#include <stdio.h>

#define CHAR_SIZE   8
#define INT_BITS sizeof(int)* CHAR_SIZE

// Function that convert Decimal to binary
void decimalToBinary(unsigned int n)
{
    int i;
    for (i = (INT_BITS -1 ); i >= 0; i--)
    {
        int k = n >> i;
        if (k & 1)
        {
            printf("1");
        }
        else
        {
            printf("0");
        }
    }
}

int main()
{
    //num for decimal number
    unsigned int num;

    printf("Enter decimal number: ");
    scanf("%u", &num);

    //Called function
    decimalToBinary(num);

    return 0;
}

Output:

Enter decimal number: 10
00000000000000000000000000001010

 

Recommended Post:

Leave a Reply

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