Conver Decimal Number into Hexa Decimal in C

Advertisements

Prev Tutorial Next Tutorial

Convert Decimal Number into Hexa Decimal Program in C

To convert decimal number to hexadecimal number in C programming, you have to ask to the user to enter the decimal number to convert it into hexadecimal number.

Decimal to Hexadecimal Conversion

Decimal number have base 10 and Hexadecimal number have base 16.

Steps to Convert Decimal number into Hexadecimal

  • Divide the number by 16.
  • Get the integer quotient for the next iteration.
  • Get the remainder for the hex digit.
  • Repeat the steps until the quotient is equal to 0.

Steps to Convert Hexadecimal to Decimal

  • Divide the original decimal number by 16
  • Divide the quotient by 16
  • Repeat the step 2 until we get quotient equal to zero.
  • Done

Decimal to Hexa Conversion Program in C

#include<stdio.h>
#include<conio.h>

void main()
{
int num,rem[20],hex=0,i=0,j;
clrscr();
printf("Enter any number: ");
scanf("%d",&num);
while(num>0)
  {
    rem[i] = num % 16;
    num = num / 16;
    i++;
  }
printf("Hexadecimal number: ");
for(j=i-1; j>=0 ; j--)
  {
    switch(rem[j])
    {
      case 10:
	  printf("A");
	  break;
      case 11:
	  printf("B");
	  break;
      case 12:
	  printf("C");
	  break;
      case 13:
	  printf("D");
	  break;
      case 14:
	  printf("E");
	  break;
      case 15:
	  printf("F");
	  break;
      default :
	 printf("%d",rem[j]);
    }
  }
getch();
}

Output

Enter any number: 231
Hexadecimal number: E7

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements