This blog is under construction

Monday 24 June 2013

C program to convert decimal to hexadecimal number

Write a C program to convert decimal to hexadecimal number.


  #include <stdio.h>
  int main() {
        int data, rem[10], count = 0;
        char hexa[] = {'A', 'B', 'C', 'D', 'E', 'F'};

        /* get the input from the user */
        printf("Enter your input:");
        scanf("%d", &data);

        /* convert decimal to hexadecimal value */
        while (data > 0) {
                /* store the reminders */
                rem[count++] = data % 16;
                data = data / 16;
        }
        printf("Hexadecimal value : ");
        while (count > 0) {
                count--;
                if (rem[count] > 9) {
                        printf("%c", hexa[rem[count] - 10]);
                } else {
                        printf("%d", rem[count]);
                }
        }

        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:18945
  Hexadecimal value : 4A01



No comments:

Post a Comment