Mon Jun 04 2018

Decimal to Hex

C Programming3366 views

File Name: decimal-to-hex.c

#include<stdio.h>

int main() {
	int decimal, i=0, value, counter = 0;
	char hex[20];
	printf("Welcome to Decimal to Binary, Octal, Hex converter\n");
	printf("Enter a deciaml number: ");
	scanf("%d",&decimal);
	while(decimal!=0) 	{
		value=(decimal%16);
		switch(value) {
			case 10: 
				hex[i]='A'; 
				break; 

			case 11:
				hex[i]='B'; 
				break; 

			case 12: 
				hex[i]='C'; 
				break; 

			case 13: 
				hex[i]='D'; 
				break; 

			case 14: 
				hex[i]='E'; 
				break; 

			case 15: 
				hex[i]='F'; 
				break; 

			default: 
				hex[i]=value+'0'; 
				break; 

		}
		decimal/=16;
		i++;
	}
	hex[i]='\0';

	printf("Hex value is: ");
	while(hex[counter] != '\0')
		counter++;
	for(i = counter-1; i >= 0; i--)
		printf("%c", hex[i]);
	printf("\n");

	return 0;
}



/* Output */
Welcome to Decimal to Hex converter
Enter a decimal number:
15

Hex value is: F
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.