-->

program to convert decimal number into Hexadecimal.

//program to convert decimal number into Hexadecimal.
#include<stdio.h>
#include<conio.h>
void main()
{
  long int decimal_number;
  printf("Enter any decimal number: ");
  scanf("%ld",&decimal_number);
  printf("Equivalent hexadecimal number is: %X",decimal_number);
getch();
}


logics in mind:

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

we can also write same program using ascii value and array.
the program goes like,
#include<stdio.h>
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: ");
    scanf("%ld",&decimalNumber);
    quotient = decimalNumber;
    while(quotient!=0){
         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
      else
         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--)
      printf("%c",hexadecimalNumber[j]);

}

                                        source:
                                         http://www.cquestions.com

No comments:

Post a Comment