Pages

This blog is under construction

Thursday, December 20, 2018

C program to convert Decimal no. to Hexadecimal no.

Example  (3509)10  =(?)16

Solution:


C Program:

#include<stdio.h>
void main()
{ int n,i=0,r;
char hex[30];
printf("\nEnter a Decimal no.");
scanf("%d",&n);
while(n>0)
{ r=n%16;
switch(r)
{ case 0: hex[i]='0'; break;
case 1: hex[i]='1'; break;
case 2: hex[i]='2'; break;
case 3: hex[i]='3'; break;
case 4: hex[i]='4'; break;
case 5: hex[i]='5'; break;
case 6: hex[i]='6'; break;
case 7: hex[i]='7'; break;
case 8: hex[i]='8'; break;
case 9: hex[i]='9'; break;
case 10: hex[i]='A'; break;
case 11: hex[i]='B'; break;
case 12: hex[i]='C'; break;
case 13: hex[i]='D'; break;
case 14: hex[i]='E'; break;
case 15: hex[i]='F'; break;
}
n=n/16;
i++;
}
printf("\nEquivalent Hexadecimal no. is: ");
for(--i;i>=0;i--)
printf("%c",hex[i]); 
    

}

Output:

Enter a Decimal no.3509                                                                                                                                                                                                      
Equivalent Hexadecimal no. is: DB5  

No comments:

Post a Comment