Tuesday, 9 December 2014

Program to convert decimal number to any other number format(hex/octal/binary)
#include<stdio.h>
#include<string.h>
main()
{
int n,i=0,a[10],choice=0,count=0;
printf("enter any number...\n");
scanf("%d",&n);
printf("enter ur choice>\n 1:decimal \n2:octal \n3:hexadecimal \n4:binary\n");
scanf("%d",&choice);
if(choice==1)
printf("decimal=%d\n",&n);
else if(choice==2)
{
while(n>0)
{
a[i]=(n%8);
n=(n/8);
i++;
count++;
}
for(i=count-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
else if(choice==3)
{
while(n>0)
{
//i=0;
a[i]=(n%16);
n=(n/16);
i++;
count++;
}
for(i=count-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
else if(choice==4)
{
int bit=31;
loop:
while(bit>=0)
{
n&(1<<bit)?printf("1"):printf("0");
bit--;
goto loop;
}
printf("\n");
}
else
printf("enter correct choice...\n");
}
OutPut:
enter any number...
144
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
2
220
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
100
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
3
64
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
100
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
4
00000000000000000000000001100100