In this tutorial, we will explain a simple program to convert the decimal number into the hexadecimal number. We have not used any number validation in this program as it is designed for the beginners for learning purpose.
You can modify this program to validate the number entered by the user during the program execution, then the user can only enter the decimal numbeDecimal to hexadecimal number conversion using c programming language to hexadecimal
Copy below c program and execute it with c compiler to see the output of the program and also start converting the number system from decimal to hexadecimal number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include<stdio.h> #include<conio.h> #include<math.h> void decimal_to_hexadecimal(long int num) { long int rem[50],i=0,length=0; while(num>0) { rem[i]=num%16; num=num/16; i++; length++; } printf("Hexadecimal number : "); for(i=length-1;i>=0;i--) { switch(rem[i]) { case 10: printf("A"); break; case 11: printf("B"); break; case 12: printf("C"); break; case 13: printf("D"); break; case 14: printf("E"); break; case 15: printf("F"); break; default : printf("%ld",rem[i]); } } } //================================================ void main() { long int num; clrscr(); printf("Enter the decimal number : "); scanf("%ld",&num); decimal_to_hexadecimal(num); getch(); } |
#First try
Please enter the decimal number: 1989
Decimal number : 7c5
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c program to convert decimal to hexadecimal, convert decimal to hex in c, decimal to hex c, decimal to hexadecimal, decimal to hexadecimal c, decimal to hexadecimal conversion in c, decimal to hexadecimal in c, hexadecimal to decimal converter, how to convert decimal to hexadecimal