Pages

Ads 468x60px

Saturday, 2 June 2012

C Program For DECIMAL TO BINARY


C Program For DECIMAL TO BINARY

C program to convert decimal to binary: c language code to convert an integer 
from decimal number system(base-10) to binary number system(base-2). Size
 of integer is assumed to be 32 bits. We use bitwise operators to perform the 
desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using
 a loop and bitwise AND the number obtained with 1(one), if the result is 1 then
 that bit is 1 otherwise it is 0(zero).



CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,z=0,i,a[20];
clrscr();
printf("Enter the n value");
scanf("%d",&n);
while(n>0)
{
a[z]=n%2;
z++;
n=n/2;
}
for(i=z;i>=0;i--)
{
printf("%d",a[i]);
}
getch();
}

0 comments:

Post a Comment

Total Pageviews