Numeric Conversions

 

/*
decimal to binary,octal and hexadecimal
*/
#include<stdio.h>
#include<conio.h>
void getBinary(int);
void getOctal(int);
void getHex(int);

void main(){
	int decimal;
	clrscr();
	printf("\nEnter the decimal input....");
	scanf("%d",&decimal);
	printf("Decimal :%d",decimal);
	getBinary(decimal);
	getOctal(decimal);
	getHex(decimal);
	getch();
}

void getBinary(int decimal){
	int remainder,quotient,binary[10],i;
	for(i=0;decimal>0;i++){
		binary[i] = decimal % 2;
		decimal = decimal/2;
	}	
	printf("\nBinary :");
	for(i=i-1;i>=0;i--){
		printf("%d",binary[i]);
	}
}

void getOctal(int decimal){
	int remainder,quotient,octal[10],i;
	for(i=0;decimal>0;i++){
		octal[i] = decimal % 8;
		decimal = decimal/8;
	}
	printf("\nOctal :");
	for(i=i-1;i>=0;i--){
		printf("%d",octal[i]);
	}
}

void getHex(int decimal){
	long remainder,quotient;
	int i;
	char hex[10];
	for(i=0;decimal>0;i++){
		remainder = decimal % 16;
		if(remainder <10 )
			hex[i]=48+remainder;
		else
			hex[i]=55+remainder;
		decimal = decimal/16;
	}
	printf("\nHexadecimal :");
	for(i=i-1;i>=0;i--){
		printf("%c",hex[i]);
	}
}