decimal to hexa Algorithm

The decimal to hexadecimal algorithm is a widely used method for converting numbers expressed in base 10 (decimal) to base 16 (hexadecimal) numeral system. This algorithm is commonly employed in the fields of computer science and mathematics, as it helps to represent large numbers more compactly and efficiently. Hexadecimal numbers are widely used in computer programming and other digital systems, as they provide a more human-readable representation of binary codes which are expressed in base 2. In the hexadecimal system, the numbers from 0 to 9 are represented by the same Arabic numerals, while the numbers 10 to 15 are represented by the English alphabet characters A to F, respectively. The decimal to hexadecimal conversion algorithm involves a sequence of division and remainder operations. To begin the conversion process, the decimal number is divided by 16, and the remainder is noted in the form of a hexadecimal digit. The quotient obtained from this division is then used as the new dividend and is again divided by 16. This process is repeated until the quotient becomes zero. The remainders obtained at each step are the hexadecimal digits of the resulting number, and they need to be arranged in reverse order of their generation, i.e., the remainder obtained in the last step becomes the leftmost digit and the first remainder becomes the rightmost digit. This sequence of hexadecimal digits represents the converted hexadecimal number equivalent to the original decimal number.
/*****Decimal to Hexadecimal conversion*******************/
#include <stdio.h>
void decimal2Hexadecimal(long num);

 
int main(){
	
  long decimalnum;
 
  printf("Enter decimal number: ");
  scanf("%ld", &decimalnum);
 
  decimal2Hexadecimal(decimalnum);

return 0;
}

/********function for convert decimal number to hexadecimal number****************/
void decimal2Hexadecimal(long num){

long decimalnum=num;
long quotient, remainder;
int i, j = 0;
char hexadecimalnum[100];

    quotient = decimalnum;

	while (quotient != 0){

        remainder = quotient % 16;
			if (remainder < 10)
            hexadecimalnum[j++] = 48 + remainder;

			else
			hexadecimalnum[j++] = 55 + remainder;

			quotient = quotient / 16;}

    // print the hexadecimal number

    for (i = j; i >= 0; i--){
            printf("%c", hexadecimalnum[i]);}

	printf("\n");
}

LANGUAGE:

DARK MODE: