program to convert decimal number into Hexadecimal.

//program to convert decimal number into Hexadecimal.
void main()
  long int decimal_number;
  printf("Enter any decimal number: ");
  printf("Equivalent hexadecimal number is: %X",decimal_number);

logics in mind:

if decimal number is 19 (base 10) then
hexadecimal number can be obtained using simply its format specifier %x.

we can also write same program using ascii value and array.
the program goes like,
void main()
    long int decimalNumber,remainder,quotient;
    int i=1,j,temp;
    char hexadecimalNumber[100];                    //array is used to store equivalent hexadecimal number
    printf("Enter any decimal number: ");
    quotient = decimalNumber;
         temp = quotient % 16;
      //To convert integer into character
      if( temp < 10)
           temp =temp + 48;          //48 is added to temp to convert number into character, look ascii table
         temp = temp + 55;                                //ascii value is used as a character after adding to number

      hexadecimalNumber[i++]= temp;
      quotient = quotient / 16;
    printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
    for(j = i -1 ;j> 0;j--)



No comments:

Post a Comment