Conversions
1) Decimal to Hexa-decimal:
#include<stdio.h>
int main()
{
int temp,n,i=0,j;
char hex[50];
printf("enter number:");
scanf("%d",&n);
while(n)
{
temp=n%16;
if(temp>9)
{
hex[i]=temp+55;
i++;
}
else
{
hex[i]=temp+48;
i++;
}
n=n/16;
}
printf("HexaDecimal Number:");
for(j=i-1;j>=0;j--)
{
printf("%c",hex[j]);
}
return 0;
}
INPUT : 100
OUTPUT : 64
///////////////////////////////////////////
2) Decimal to Octal:
#include<stdio.h>
int main()
{
int temp,n,i=0,j;
char octal[50];
printf("enter number");
scanf("%d",&n);
while(n)
{
temp=n%8;
octal[i]=temp+48;
i++;
n=n/8;
}
printf("Octal Number:");
for(j=i-1;j>=0;j--)
{
printf("%c",octal[j]);
}
return 0;
}
INPUT : 100
OUTPUT : 144
///////////////////////////////////////////
3) Hexa-decimal To Decimal:
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include<math.h>
int
main()
{
char
a[100];
int
i,dec=0,j=0;
printf("enter Hexadecimal number:");
gets(a);
for(i=strlen(a)-1;i>=0;i--)
{
if(a[i]>57)
{
dec=dec+(a[i]-55)*pow(16,j);
j++;
}
else
{
dec=dec+(a[i]-48)*pow(16,j);
j++;
}
}
printf("Decimal number=%d",dec);
return 0;
}
INPUT : 64
OUTPUT : 100
///////////////////////////////////////////
4) Octal To Decimal:
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include<math.h>
int
main()
{
char a[100];
int i,dec=0,j=0;
printf("enter Octal number\n");
gets(a);
for(i=strlen(a)-1;i>=0;i--)
{
dec=dec+(a[i]-48)*pow(8,j);
j++;
}
printf("Decimal Number=%d",dec);
int i,dec=0,j=0;
gets(a);
for(i=strlen(a)-1;i>=0;i--)
{
dec=dec+(a[i]-48)*pow(8,j);
j++;
}
printf("Decimal Number=%d",dec);
return 0;
}
INPUT : 144
OUTPUT : 100
5) Octal To Hexa-Decimal:
#include<stdio.h>
#include<string.h>
#include<math.h>
int oct_to_dec(char *octal) // octal to decimal conversion
{
int j,i=0,d=0;
for(j=strlen(octal)-1;j>=0;j--)
{
d=d+(octal[j]-48)*pow(8,i);
i++;
}
return d;
}
void dec_to_hex(int x) // decimal to hexa-decimal conversion
{
char hexadecimal[100];
int temp,i=0,j;
while(x)
{
temp=x%16;
if(temp>9)
{
hexadecimal[i]=temp+55;
i++;
}
else
{
hexadecimal[i]=temp+48;
i++;
}
x/=16;
}
hexadecimal[i]='\0';
printf("hexadecimal:");
for(j=strlen(hexadecimal)-1;j>=0;j--)
{
printf("%c",hexadecimal[j]);
}
}
int main()
{
char oct_n[100];
int dec,j;
printf("enter octal number:");
gets(oct_n);
dec=oct_to_dec(oct_n);
dec_to_hex(dec);
}
///////////////////////////////////////////
5) Octal To Hexa-Decimal:
#include<stdio.h>
#include<string.h>
#include<math.h>
int oct_to_dec(char *octal) // octal to decimal conversion
{
int j,i=0,d=0;
for(j=strlen(octal)-1;j>=0;j--)
{
d=d+(octal[j]-48)*pow(8,i);
i++;
}
return d;
}
void dec_to_hex(int x) // decimal to hexa-decimal conversion
{
char hexadecimal[100];
int temp,i=0,j;
while(x)
{
temp=x%16;
if(temp>9)
{
hexadecimal[i]=temp+55;
i++;
}
else
{
hexadecimal[i]=temp+48;
i++;
}
x/=16;
}
hexadecimal[i]='\0';
printf("hexadecimal:");
for(j=strlen(hexadecimal)-1;j>=0;j--)
{
printf("%c",hexadecimal[j]);
}
}
int main()
{
char oct_n[100];
int dec,j;
printf("enter octal number:");
gets(oct_n);
dec=oct_to_dec(oct_n);
dec_to_hex(dec);
}
INPUT : 144
OUTPUT : 64
///////////////////////////////////////////
6) Hexa-Decimal To Octal :
#include<stdio.h>
#include<string.h>
#include<math.h>
int hex_to_dec(char *hexa) // Hex to decimal conversion
{
int j,i=0,d=0;
for(j=strlen(hexa)-1;j>=0;j--)
{
d=d+(hexa[j]-48)*pow(16,i);
i++;
}
return d;
}
void dec_to_oct(int x) // Decimal to octal conversion
{
char octal[100];
int temp,i=0,j;
while(x)
{
temp=x%8;
octal[i]=temp+48;
i++;
x/=8;
}
octal[i]='\0';
printf("octal:");
for(j=strlen(octal)-1;j>=0;j--)
{
printf("%c",octal[j]);
}
}
int main()
{
char hex_n[100];
int dec,j;
printf("enter Hexa-Decimal Number:");
gets(hex_n);
dec=hex_to_dec(hex_n);
dec_to_oct(dec);
}
///////////////////////////////////////////
6) Hexa-Decimal To Octal :
#include<stdio.h>
#include<string.h>
#include<math.h>
int hex_to_dec(char *hexa) // Hex to decimal conversion
{
int j,i=0,d=0;
for(j=strlen(hexa)-1;j>=0;j--)
{
d=d+(hexa[j]-48)*pow(16,i);
i++;
}
return d;
}
void dec_to_oct(int x) // Decimal to octal conversion
{
char octal[100];
int temp,i=0,j;
while(x)
{
temp=x%8;
octal[i]=temp+48;
i++;
x/=8;
}
octal[i]='\0';
printf("octal:");
for(j=strlen(octal)-1;j>=0;j--)
{
printf("%c",octal[j]);
}
}
int main()
{
char hex_n[100];
int dec,j;
printf("enter Hexa-Decimal Number:");
gets(hex_n);
dec=hex_to_dec(hex_n);
dec_to_oct(dec);
}
INPUT : 64
OUTPUT : 144
Comments
Post a Comment