Tuesday 5 February 2013

Write a c program to convert given decimal number into binary number



#include<stdio.h>
#include<conio.h>
   void main()
{
   int a[10],n,i,j=0,rem;
   clrscr();
   printf("\nEnter the Decimal Number: ");
   scanf("%d",&n);
   while(n!=1)
{
   rem=n%2;
   printf("\n%d",rem);
   a[j]=rem;
   n=n/2;
   printf("\n%d%d",a[j],n);
   j++;
}
   a[j]=1;
   printf("\nBinary Number is: ");
   for(i=j;i>=0;i--)
   printf("%d",a[i]);
   getch();
}

//OUTPUT:
//Enter the Decimal Number: 12
    6 0
    3 0
    1 1
    1 1
//Binary Number is: 1100

1 comment:

  1. Decimal number system is a base 10 number system using digits for 0 to 9 whereas binary number system is base 2 and uses 0 and 1. To convert Decimal to Binary number we have to convert from base 10 to base 2.

    ReplyDelete