Skip to content

Instantly share code, notes, and snippets.

@abrarShariar
Created January 24, 2016 08:32
Show Gist options
  • Save abrarShariar/c7de95d733c7662700fb to your computer and use it in GitHub Desktop.
Save abrarShariar/c7de95d733c7662700fb to your computer and use it in GitHub Desktop.
Conversion (Decimal,Binary,Hexa)
#include<stdio.h>
#include<math.h>
void main(){
int x;
printf("Press 1 for Dec to Bin \nPress 2 for Dec to Hexa \nPress 3 for Dec to Oct\n");
printf("Press 4 for Bin to Dec \nPress 5 for Bin to Hexa \nPress 6 for Bin to Oct\n");
printf("Press 7 for Oct to Dec \nPress 8 for Oct to Hexa \nPress 9 for Oct to Bin\n");
printf("Press 10 for Hexa to Dec \nPress 11 for Hexa to Bin \nPress 12 for Hexa to Oct\n\n");
scanf("%d",&x);
switch(x){
case 1:
dec_bin();
break;
case 2:
dec_hex();
break;
case 3:
dec_oct();
break;
case 4:
bin_dec();
break;
case 5:
bin_hex();
break;
case 6:
bin_oct();
break;
case 7:
oct_dec();
break;
case 8:
oct_hex();
break;
case 9:
oct_bin();
break;
case 10:
hex_dec();
break;
case 11:
hex_bin();
break;
case 12:
hex_oct();
break;
default:
printf("INVALID");
}
printf("\n\n");
}
//decimal to binary
void dec_bin(){
int dec,bin[100],i=0,rem,j;
printf("Decimal: ");
scanf("%d",&dec);
while(dec!=0){
rem=dec%2;
bin[i]=rem;
i++;
dec=dec/2;
}
printf("Binary: ");
for(j=i-1;j>=0;j--){
printf("%d",bin[j]);
}
}
//decimal to octal
void dec_oct(){
int dec,rem,i=0,oct[100],j;
printf("Decimal: ");
scanf("%d",&dec);
while(dec!=0){
rem=dec%8;
oct[i]=rem;
i++;
dec=dec/8;
}
printf("Octal: ");
for(j=i-1;j>=0;j--){
printf("%d",oct[j]);
}
}
//decimal to hexadecimal
void dec_hex(){
int dec,rem,i=0,j,hex[100];
//hexadecimal table
int table[16]={48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70};
printf("Decimal: ");
scanf("%d",&dec);
while(dec!=0){
rem=dec%16;
hex[i]=table[rem];
i++;
dec=dec/16;
}
printf("Hexadecimal: ");
for(j=i-1;j>=0;j--){
printf("%c",hex[j]);
}
}
//binary to decimal
void bin_dec(){
int bin,rem,dec=0,i=0;
printf("Binary: ");
scanf("%d",&bin);
while(bin!=0){
rem=bin%10;
dec=dec+rem*pow(2,i);
i++;
bin=bin/10;
}
printf("Decimal: %d",dec);
}
//binary to hexadecimal
void bin_hex(){
int bin,rem,dec=0,i=0,j=0,k,hex[100];
//hexa table
int table[16]={48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70};
printf("Binary: ");
scanf("%d",&bin);
//bin to dec
while(bin!=0){
rem=bin%10;
dec=dec+rem*pow(2,i);
i++;
bin=bin/10;
}
//dec to hexa
while(dec!=0){
rem=dec%16;
hex[j]=table[rem];
j++;
dec=dec/16;
}
printf("Hexadecimal: ");
for(k=j-1;k>=0;k--){
printf("%c",hex[k]);
}
}
//binary to octal
void bin_oct(){
int bin,dec=0,rem,i=0,k,oct[100],j=0;
printf("Binary: ");
scanf("%d",&bin);
//bin to dec
while(bin!=0){
rem=bin%10;
dec=dec+rem*pow(2,i);
i++;
bin=bin/10;
}
//dec to bin
while(dec!=0){
rem=dec%8;
oct[j]=rem;
j++;
dec=dec/8;
}
printf("Octal: ");
for(k=j-1;k>=0;k--){
printf("%d",oct[k]);
}
}
//octal to decimal
void oct_dec(){
int oct,rem,dec=0,i=0;
printf("Octal: ");
scanf("%d",&oct);
while(oct!=0){
rem=oct%10;
dec=dec+rem*pow(8,i);
i++;
oct=oct/10;
}
printf("Decimal: %d",dec);
}
//octal to hexadecimal
void oct_hex(){
int oct,dec=0,i=0,j=0,k,rem,hex[100];
//hexadecimal table
int table[16]={48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70};
printf("Octal: ");
scanf("%d",&oct);
//oct to dec
while(oct!=0){
rem=oct%10;
dec=dec+rem*pow(8,i);
i++;
oct=oct/10;
}
//dec to hex
while(dec!=0){
rem=dec%16;
hex[j]=table[rem];
j++;
dec=dec/16;
}
printf("Hexadecimal: ");
for(k=j-1;k>=0;k--){
printf("%c",hex[k]);
}
}
//octal to binary
void oct_bin(){
int oct,rem,dec=0,i=0,j=0,k,bin[100];
printf("Octal: ");
scanf("%d",&oct);
//oct to dec
while(oct!=0){
rem=oct%10;
dec=dec+rem*pow(8,i);
i++;
oct=oct/10;
}
//dec to bin
while(dec!=0){
rem=dec%2;
bin[j]=rem;
j++;
dec=dec/2;
}
printf("Binary: ");
for(k=j-1;k>=0;k--){
printf("%d",bin[k]);
}
}
//hexadecimal to decimal
void hex_dec(){
char c;
int hex[100];
int i=0,j,k,temp;
//hexa table
int table[16]={48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70};
//hex input
fflush(stdin);
printf("Hexadecimal: ");
while((c=getchar())!='\n'){
hex[i]=c;
i++;
}
//making input array coresponding to hexa table
for(j=0;j<i;j++){
temp=hex[j];
for(k=0;k<16;k++){
if(table[k]==temp){
hex[j]=k;
break;
}
}
}
//hexa to dec
int dec=0,n=0;
for(j=i-1;j>=0;j--){
dec=dec+hex[j]*pow(16,n);
n++;
}
//output
printf("Decimal: %d",dec);
}
//hexadecimal to binary
void hex_bin(){
char c;
int hex[100];
int i=0,j,k,temp;
//hexa table
int table[16]={48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70};
//hex input
fflush(stdin);
printf("Hexadecimal: ");
while((c=getchar())!='\n'){
hex[i]=c;
i++;
}
//making input array coresponding to hexa table
for(j=0;j<i;j++){
temp=hex[j];
for(k=0;k<16;k++){
if(table[k]==temp){
hex[j]=k;
break;
}
}
}
//hexa to dec
int dec=0,n=0;
for(j=i-1;j>=0;j--){
dec=dec+hex[j]*pow(16,n);
n++;
}
//dec to bin
int rem,bin[100],b=0;
while(dec!=0){
rem=dec%2;
bin[b]=rem;
b++;
dec=dec/2;
}
//output
printf("Binary: ");
for(j=b-1;j>=0;j--){
printf("%d",bin[j]);
}
}
//hexadecimal to octal
void hex_oct(){
char c;
int hex[100],i=0,j,k,temp;
int table[16]={48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70};
fflush(stdin);
//hexa input
printf("Hexadecimal: ");
while((c=getchar())!='\n'){
hex[i]=c;
i++;
}
//converting to hexa table
for(j=0;j<i;j++){
temp=hex[j];
for(k=0;k<16;k++){
if(table[k]==temp){
hex[j]=k;
break;
}
}
}
//hex to dec
int dec=0,n=0;
for(j=i-1;j>=0;j--){
dec=dec+hex[j]*pow(16,n);
n++;
}
//dec to oct
int oct[100],o=0,rem;
while(dec!=0){
rem=dec%8;
oct[o]=rem;
o++;
dec=dec/8;
}
//output
printf("Octal: ");
for(j=o-1;j>=0;j--){
printf("%d",oct[j]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment