Friday, September 21, 2012

program for decimal to binary conversion

1)program for decimal to binary conversion
#include<conio.h>
#include<stdio.h>
void gbit(int num);
void main()
{
int num;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
printf("\n\n");
gbit(num);
getch();
}
void gbit(int num)
{
int temp;
if(num)
{
temp=num%2;
gbit(num/=2);
printf("%d",temp);

}
}

2)
 #include<stdio.h>
#include<conio.h>
void main()
{
int n,a[30],r,i=0,j;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);  //reads the decimal number
printf("the binary number requred is\n");
while(n)
{
a[i]=n%2;
n/=2;
i++;
}
j=i-1;
for(i=j;i>0;i--)
{
printf("%d",a[i]);  //prints the binary number
}


getch();
}


No comments:

Post a Comment