Home / Programs / Write a program that will compute the binary equivalent number of a decimal number and print it as output.
Programming Example

Write a program that will compute the binary equivalent number of a decimal number and print it as output.

👁 871 Views
💻 Practical Program
📘 Step by Step Learning
Write a program that will compute the binary equivalent number of a decimal number and print it as output.

Program Code

#include <stdio.h>

int main()
{

  int a[20],i,m,n,r;
  printf("\n Enter the decimal Integer  ");

  scanf("%d",&n);
  m=n;

  for(i=0;n>0;i++)
  {
    r=n%2;
    a[i]=r;
    n=n/2;
   }

  printf("\n Binary equivalent of %d is \t",m);

  for(i--;i>=0;i--)
    printf("%d",a[i]);

  return 0;

}



Output


 Enter the decimal Integer  10

 Binary equivalent of 10 is     1010
--------------------------------
Process exited after 3.289 seconds with return value 0
Press any key to continue . . .

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.