Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,234 questions

56,136 answers

573 users

How to convert input decimal value to hexadecimal in C

1 Answer

0 votes
#include <stdio.h> 

void decimal_to_hex(int d);

int main(void)
{   
    int d;

    printf("Enter a number: ");
    scanf("%d",&d);
    decimal_to_hex(d);
 
    return 0;
}

void decimal_to_hex(int d)
{
      int n = d, digits = 0, arr[5], i = 0;
      
      while (n>15)
      {
           arr[i]=n%16;
           n=n/16;
           i++;
           digits++;
      }
      arr[i]=n;
      for (i = digits; i >=0; i--)
      {
           if (arr[i]==10)
                printf("A");
           else if (arr[i]==11)
                printf("B");
           else if (arr[i]==12)
                printf("C");
           else if (arr[i]==13)
                printf("D");
           else if (arr[i]==14)
                printf("E");
           else if (arr[i]==15)
                printf("F");
           else
                printf("%d",arr[i]);
      }
}
 
/*
run:
   
Enter a number: 255
FF

*/

/*
run:
 
Enter a number: 45
2D

*/


 



answered Nov 9, 2016 by avibootz
edited Nov 9, 2016 by avibootz

Related questions

...