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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 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

3 answers 162 views
162 views asked Aug 24, 2021 by avibootz
1 answer 110 views
110 views asked Aug 24, 2021 by avibootz
2 answers 184 views
1 answer 169 views
1 answer 155 views
1 answer 161 views
...