All for Joomla All for Webmasters

C Program to convert Decimal to Binary

Decimal to binary in C: We can convert any decimal number (base-10 (0 to 9)) into binary number(base-2 (0 or 1)) by c program.

Decimal Number

Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.

Binary Number

Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.

Let’s see the some binary numbers for the decimal number.

Decimal Binary
1 0
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010

Decimal to Binary Conversion Algorithm

  • Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array
  • Step 2: Divide the number by 2 through / (division operator)
  • Step 3: Repeat the step 2 until number is greater than 0

Let’s see the c example to convert decimal to binary.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. int a[10],n,i;
  6. clrscr();
  7. printf(“Enter the number to convert: “);
  8. scanf(“%d”,&n);
  9. for(i=0;n>0;i++)
  10. {
  11. a[i]=n%2;
  12. n=n/2;
  13. }
  14. printf(“\nBinary of Given Number is=”);
  15. for(i=i-1;i>=0;i–)
  16. {
  17. printf(“%d”,a[i]);
  18. }
  19. getch();
  20. }

Output:

Enter the number to convert: 5
Binary of Given Number is=101
PinIt
submit to reddit

Leave a Reply

Top