Tuesday, September 27, 2011

C Program To Convert Decimal To Binary Octal And Hexadecimal

Variables used
b,c,d -Array
a -Decimal number
x,y -a
i,j -Loop control
l,k -Limits

Program
#include<stdio.h>
main()
{
system ("clear");
int a,x,y,b[100],c[100],d[100],i=0,k=0,l=0,j,n;
printf("Enter the number\n");
scanf("%d",&a);
x=a;
y=a;
printf("Enterthe choice\n 1.Binary 2.Octal 3.Hexadecimal\n\n");
scanf("%d",&n);
{
switch(n)
{case 1:
while(a>0)
{
b[i]=a%2;
a=a/2;
i++;
}
printf("Binary equalent is \n\n");
for(j=i-1;j>=0;j--)
printf("%d",b[j]);
break;
case 2:
while(x>0)
{
c[k]=x%8;
x=x/8;
k++;
}
printf("Octal equalent is \n\n");
for(j=k-1;j>=0;j--)
printf("%d",c[j]);
break;
case 3:
while(y>0)
{
d[l]=y%16;
y=y/16;
l++;
}
printf("Hexadecimal is \n\n");
for(j=l-1;j>=0;--j)
{
switch(d[j])
{
case 10:printf("A");
break;
case 11:printf("B");
break;
case 12:printf("C");
break;
case 13:printf("D");
break;
case 14:printf("E");
break;
case 15:printf("F");
break;
default:printf("%d",d[j]);


} } } } }

Output


Enter the number 
10
Enterthe choice
1.Binary 2.Octal 3.Hexadecimal
1
Binary equalent is  1010

No comments:

Post a Comment