Write a menu driven program to convert a decimal number to binary or octal or hexadecimal as required by user.

#include<stdio.h>
 #include<stdlib.h>

long dec_to_bin(long);
 long dec_to_oct(long);
 long dec_to_hexa(long);
int main()
 {
 int i,j,q;
 printf("Press 1 for DEC to BIN\n");
 printf("Press 2 for DEC to OCT\n");
 printf("Press 3 for DEC to HEXA\n");
scanf("%d",&i);
switch(i)
 {
 case 1:
 long k,j;
 printf("Enter the no.");
 scanf("%d",&j);
 printf("The Equivalent Binary is\n ");
 dec_to_bin(j);
break;
 case 2:
 long y,x;
 printf("Enter the no.");
 scanf("%d",&x);
 printf("The Equivalent octal is\n ");
 dec_to_oct(x);
break;
 case 3:
 long g,h;
 printf("Enter the no.");
 scanf("%d",&h);
 printf("The Equivalent hexadecimal is\n ");
 dec_to_hexa(h);
break;
 default:
 break;
}
system("Pause");
 return 0;
 }
long dec_to_bin(long i)
{
long j[100],k,l=0,m;
 j[0]=k=1;
 while(i!=0)
 {
 j[l]=i%2;
 i=int(i/2);
l++;
 }
for(m=l-1;m>=0;m--)
 {
 printf("%d ",j[m]);
 }
return 0;
 }
 long dec_to_oct(long i)
 {
long j[100],k,l,m;
 j[0]=k=1;
 l=0;
 while(i!=0)
 {
 j[l]=i%8;
i=int(i/8);
l++;
 }
for(m=l;m>=0;m--)
printf("%d",j[m]);
 return 0;
 }
long dec_to_hexa(long i)
 {
char j[100],k,l=0,m,n=65;
j[0]=k=1;
 while(i!=0)
 {
 j[l]=i%16;
 i=int(i/16);
l++;
 }
 for(m=l-1;m>=0;m--)
 {
if(j[m]>=10)
 {
n=n+j[m]-10;
 printf("%c\n",n);
 }
else
 {
 printf("%d\n",j[m]);
 }
 }
 return 0;
 }
Advertisements

Leave a comment

Filed under TUTORIALS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s