CONVERSION OF DECIMAL,HEXADECIMAL AND OCTAL
TO BINARY
PROGRAM:
#include<stdio.h>
char num[100];
void decimalto(int);
void octalto(int);
void hexto(int);
void main()
{
int len=0,i=0,c;
printf("MENU\n1. DECIMAL\n2. HEXADECIMAL\n3. OCTAL\n");
printf("ENTER YOUR CHOICE\n");
scanf("%d",&c);
printf("\nEnter number\n");
scanf("%s",num);
while(num[i]!='\0')
{
len++;
i++;
}
switch(c)
{
case 1: decimalto(len);
break;
case 2: hexto(len);
break;
case 3: octalto(len);
break;
default: printf("\n wrong choice\n");
break;
}
}
void decimalto(int len)
{
int i,a[10],c=0,d=0,flag=0,bin[20];
for(i=0;i<len;i++)
{
if((num[i]<48) || (num[i]>57))
flag=1;
}
if(flag==0)
{
for(i=0;i<len;i++)
{
a[i]=num[i]-48;
d=d*10+a[i];
}
while(d!=0)
{
bin[c]=d%2;
d=d/2;
c++;
}
printf("\nbinary number is\n");
for(i=c-1;i>=0;i--)
printf("%d",bin[i]);
}
else
printf("\ninvalid number\n");
}
void octalto(int len)
{
int i,a[20],aa[20],j=0,n,d,k,flag=0;
for(i=0;i<len;i++)
a[i]=num[i]-48;
for(i=0;i<len;i++)
{
if(a[i]>7)
flag=1;
}
if(flag==0)
{
printf("\n binary number is\n");
for(i=0;i<len;i++)
{
n=a[i];
for(j=0;j<3;j++)
{
d=n%2;
n=n/2;
aa[j]=d;
}
for(k=j-1;k>=0;k--)
printf("%d",aa[k]);
}
}
else
printf("invalid number\n");
}
void hexto(int len)
{
int i,a[20],aa[20],j=0,n,d,k,flag=0;
for(i=0;i<len;i++)
{
if(num[i]<=57)
a[i]=num[i]-48;
else
a[i]=num[i]-55;
}
for(i=0;i<len;i++)
{
if(a[i]>15)
flag=1;
}
if(flag==0)
{
printf("\n binary number is\n");
for(i=0;i<len;i++)
{
n=a[i];
for(j=0;j<4;j++)
{
d=n%2;
n=n/2;
aa[j]=d;
}
for(k=j-1;k>=0;k--)
printf("%d",aa[k]);
}
}
else
printf("\ninvalid number\n");
}
OUTPUT:
MENU
1. DECIMAL
2. HEXADECIMAL
3. OCTAL
ENTER YOUR CHOICE
1
Enter number
52
Binary number is
110100
MENU
1. DECIMAL
2. HEXADECIMAL
3. OCTAL
ENTER YOUR CHOICE
2
Enter number
A23
Binary number is 101000100011
MENU
1. DECIMAL
2. HEXADECIMAL
3. OCTAL
ENTER YOUR CHOICE
3
Enter number
7
Binary number is
111
TO BINARY
PROGRAM:
#include<stdio.h>
char num[100];
void decimalto(int);
void octalto(int);
void hexto(int);
void main()
{
int len=0,i=0,c;
printf("MENU\n1. DECIMAL\n2. HEXADECIMAL\n3. OCTAL\n");
printf("ENTER YOUR CHOICE\n");
scanf("%d",&c);
printf("\nEnter number\n");
scanf("%s",num);
while(num[i]!='\0')
{
len++;
i++;
}
switch(c)
{
case 1: decimalto(len);
break;
case 2: hexto(len);
break;
case 3: octalto(len);
break;
default: printf("\n wrong choice\n");
break;
}
}
void decimalto(int len)
{
int i,a[10],c=0,d=0,flag=0,bin[20];
for(i=0;i<len;i++)
{
if((num[i]<48) || (num[i]>57))
flag=1;
}
if(flag==0)
{
for(i=0;i<len;i++)
{
a[i]=num[i]-48;
d=d*10+a[i];
}
while(d!=0)
{
bin[c]=d%2;
d=d/2;
c++;
}
printf("\nbinary number is\n");
for(i=c-1;i>=0;i--)
printf("%d",bin[i]);
}
else
printf("\ninvalid number\n");
}
void octalto(int len)
{
int i,a[20],aa[20],j=0,n,d,k,flag=0;
for(i=0;i<len;i++)
a[i]=num[i]-48;
for(i=0;i<len;i++)
{
if(a[i]>7)
flag=1;
}
if(flag==0)
{
printf("\n binary number is\n");
for(i=0;i<len;i++)
{
n=a[i];
for(j=0;j<3;j++)
{
d=n%2;
n=n/2;
aa[j]=d;
}
for(k=j-1;k>=0;k--)
printf("%d",aa[k]);
}
}
else
printf("invalid number\n");
}
void hexto(int len)
{
int i,a[20],aa[20],j=0,n,d,k,flag=0;
for(i=0;i<len;i++)
{
if(num[i]<=57)
a[i]=num[i]-48;
else
a[i]=num[i]-55;
}
for(i=0;i<len;i++)
{
if(a[i]>15)
flag=1;
}
if(flag==0)
{
printf("\n binary number is\n");
for(i=0;i<len;i++)
{
n=a[i];
for(j=0;j<4;j++)
{
d=n%2;
n=n/2;
aa[j]=d;
}
for(k=j-1;k>=0;k--)
printf("%d",aa[k]);
}
}
else
printf("\ninvalid number\n");
}
OUTPUT:
MENU
1. DECIMAL
2. HEXADECIMAL
3. OCTAL
ENTER YOUR CHOICE
1
Enter number
52
Binary number is
110100
MENU
1. DECIMAL
2. HEXADECIMAL
3. OCTAL
ENTER YOUR CHOICE
2
Enter number
A23
Binary number is 101000100011
MENU
1. DECIMAL
2. HEXADECIMAL
3. OCTAL
ENTER YOUR CHOICE
3
Enter number
7
Binary number is
111
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.
0 comments:
Post a Comment