C Programming

Decimal to Hex

Learn C programming for convert decimal number to hex number

6/4/2018
0 views
decimal-to-hex.cC
#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
C programmingdecimal to hex conversationdecimal to hex converter

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com