C Program To Convert Decimal To Binary Numbers

C Program To Convert Decimal To Binary.  If you are looking for a C program to convert decimal number to binary number example, this C programming tutorial will help you to learn how to write a program for decimal to binary in C. Just go through this C programming example to learn about decimal to binary conversion.

C Program To Convert Decimal To Binary

Learn how to write a c Program to convert decimal number to binary number. Writing decimal to binary conversion program in C can be done using various techniques but here in this program, we show how to write a c program for decimal to binary in a proper way. Happy coding.

Decimal To Binary Conversion Examples

Here coding compiler will give you the decimal to binary conversion examples. A decimal number consists of base 10, i.e (0 to 9) and a binary number consists of base 2, i.e (0 and 1). Use this decimal to binary converter tool to convert numbers.

Decimal To Binary Conversion

  • Decimal – Binary
  •      5       –  101
  •     15      –  1111
  •    25      –   11001
  •    50     –    110010
  •    75     –    1001011
  •    99    –     1100011

C Program For Decimal To Binary Conversion Code

You can copy paste the below C Program For Decimal To Binary Conversion Using while loop, in c compiler to check how the source code work. Or write your own decimal to binary C Program with the help of this below c programming tutorial.

Source Code:

/* C program to convert decimal to binary - Decimaltobinary.c */


#include <stdio.h>
 
void main()
{
  //variable declaration
  long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0;
 
  //asking user to enter decimal number
  printf("Enter a decimal number \n");
  //reading decimal number from uesr
  scanf("%ld", &num);
  decimal_num = num;
  //c programming logic to convert decimal to binary
  while (num > 0) //while loop
  {
    remainder = num % 2;
    /* To count nuber of one's in decimal umber */
    if (remainder == 1) //if statement
    {
     no_of_1s++;
    }
    binary = binary + remainder * base;
    num = num / 2;
    base = base * 10;
  }
  //printing output elements
  printf("Entered decimal number is = %d\n", decimal_num);
  printf("It's Binary equivalent number is = %ld\n", binary);
  printf("Number of 1's in the binary number is = %d\n", no_of_1s);
}

C Program To Convert Decimal Number To Binary Number Output

After you compile and run the above c program to convert decimal number to binary number, your C compiler asks you to enter the decimal number to convert into binary number. After you enter a decimal integer, the program will be executed and give output as a binary number.

Output:

Entered decimal number is = 99

It’s Binary equivalent number is = 1100011

Number of 1’s in the binary number is = 4

C PROGRAMMING Examples

Leave a Comment