Decimal to hexadecimal in C
In this post, you will learn how to convert decimal to hexadecimal using C programming language.
A decimal number is a base-10 number system where the range of numbers is 0,1,2,3,4,5,6,7,8,9. The main advantages of Decimal Number System are easy-to-read, used by humans and easy to manipulate. But there are some disadvantages, like wastage of space and time.
Hexadecimal describes a base-16 number system. Hexadecimal Number System is generally used in computer programming and Microprocessors. It is also helpful to describe colours on web pages. The main advantage of a Hexadecimal Number is that it is exceptionally compact and by utilizing a base of 16 means that the number of digits used to address a given number is normally less than in binary or decimal.
Convert decimal to hexadecimal in C using Loop
For converting a decimal to hexadecimal, the standard mathematical approach is to divide the number by 16 until it reduces to zero. The sequence of remainders from last to first in a hex structure is the hexadecimal number system of the given decimal number. The following example demonstrates this -
#include <stdio.h>
int main() {
long int decimalnum, quotient;
int a = 1 , b , num;
char hexadecimal[ 100 ];
printf("Please enter decimal number: ");
scanf("%ld" , &decimalnum);
quotient = decimalnum ;
while( quotient != 0 ) {
num = quotient % 16 ;
if( num < 10 )
num = num + 48 ;
else
num = num + 55 ;
hexadecimal[a++]= num;
quotient = quotient / 16;
}
printf("Decimal to hexadecimal: ");
for(b = a -1 ; b > 0 ; b-- )
printf( "%c" , hexadecimal[ b ] );
return 0 ;
}
Output of the above code:
Please enter decimal number: 88
Decimal to hexadecimal: 58
Please enter decimal number: 66
Decimal to hexadecimal: 42
Convert decimal to hexadecimal in C using switch
In the given C program, we have used the switch statement to convert a decimal to hexadecimal number.
#include <stdio.h>
int main()
{
int decimalnum, temp, x;
char hex [32];
printf("Please enter the decimal number: ") ;
scanf("%d", &decimalnum) ;
temp = 0 ;
while(decimalnum > 0)
{
switch(decimalnum % 16)
{
case 10 :
hex [temp] = 'A' ; break;
case 11 :
hex [temp] = 'B'; break;
case 12 :
hex [temp] = 'C' ; break;
case 13 :
hex [temp] = 'D'; break;
case 14 :
hex [temp] = 'E' ; break;
case 15 :
hex [temp] = 'F'; break;
default :
hex [temp] = (decimalnum%16) + 0x30;
}
decimalnum = decimalnum / 16;
temp++ ;
}
printf("Decimal to hexadecimal: ");
for( x = (temp-1) ; x >= 0 ; x--)
printf("%c" , hex[x]);
return 0;
}
Output of the above code:
Please enter the decimal number: 66
Decimal to hexadecimal: 42
Please enter the decimal number: 88
Decimal to hexadecimal: 58
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative