Decimal to Hex

C Programming

C Programming Examples

#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

Learn more about Decimal and Hexadecimal