Thursday, 24 January 2013

Conversion of decimal to binary,octal,hexadecimal in C/C++


#include<stdio.h>
#include<conio.h>
void main()
{
int s,a,k,c,i,b[10],m[10];
int n=2;
clrscr();
i=0;
c=0;
printf("enter a number:");
scanf("%d",&a);
while(a>0)
{
b[i]=a%n;

a=a/n;

i++;
c++;

}
k=0;
for(i=c-1;i>=0;i--)
{
m[k]=b[i];
k++;
}
for(i=0;i<c;i++)
{
printf("%d",m[i]);
}

getch();
}

input: 9
output:1001
if you want to calculate octal then replace n=8, for hexadecimal, n=16;

1 comment:

  1. for hexadecimal the code doesn't work..eg. if i/p is 15 it must print F or f .

    ReplyDelete