Tag: c conversion programs

  • C Program to Convert Decimal to Binary

    In this tutorial, we will write a C program to convert decimal to binary. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed by the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 5
    Output: 101

    Input: 15
    Output: 1111

    Now let us go through a program for the conversion of decimal to binary in C using while loop.


    C Program to Convert Decimal to Binary using While loop

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int deciNum, binaryNum[30], i = 0;
    
      printf("Enter a decimal number: ");
      scanf("%d", &deciNum);
    
      while (deciNum != 0)
      {
        binaryNum[i] = deciNum % 2;
        i++;
        deciNum = deciNum / 2;
      }
    
      printf("Equivalent Binary Value: ");
      for (i = (i - 1); i >= 0; i--)
        printf("%d", binaryNum[i]);
    
      getch();
      return 0;
    }

    Output:

    Enter a decimal number: 13
    Equivalent Binary Number: 1101


    C Program to Convert Decimal to Binary without using Array

    Source code: Decimal to Binary without using Array in C.

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int deciNum, binaryNum = 0, mul = 1, rem;
    
      printf("Enter any decimal number: ");
      scanf("%d", &deciNum);
    
      while (deciNum > 0)
      {
        rem = deciNum % 2;
        binaryNum = binaryNum + (rem *mul);
        mul = mul * 10;
        deciNum = deciNum / 2;
      }
    
      printf("Equivalent Binary Value: %d", binaryNum);
    
      getch();
      return 0;
    }

    After the successful execution of the above program, it will produce the same output as the above program.


  • C Program to Convert Binary to Decimal

    In this tutorial, we will write a program to convert binary to decimal in C. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed by the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 101
    Output: 5

    Input: 111
    Output: 7

    Now let us go through a program for the conversion of binary to decimal in C using while loop.


    C Program to Convert Binary to Decimal using while loop

    In this program a math function pow() is used, it gives the power calculation and a header file math.h is required to add the beginning of the program.

    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    int main()
    {
      int binaryNum, deciNum = 0, i = 0, rem;
    
      printf("Enter a binary number: ");
      scanf("%d", &binaryNum);
    
      while (binaryNum != 0)
      {
        rem = binaryNum % 10;
        deciNum = deciNum + rem* pow(2, i);
        i++;
        binaryNum = binaryNum / 10;
      }
    
      printf("Equivalent Decimal Value: %d", deciNum);
    
      getch();
      return 0;
    }

    Output:

    Enter a binary number: 1111
    Equivalent Decimal Value: 15


    Convert Binary to Decimal in C without pow() Function

    Although if you do not want to use the pow() function then you simply replace the while loop of the above program with the following while loop code and others remain the same.

    while (binnum != 0)
    {
      rem = binnum % 10;
      decnum = decnum + (rem *i);
      i = i * 2;
      binnum = binnum / 10;
    }

    It will produce the same output as the above. Also, you do not need to include math.h header file.