Program to Convert Decimal number into Hexadecimal : Number System : Download
Logic of This Program :
- Accept Number from User
- As we are dividing number by 16 , we have only remainders 0 to 15 etc
- Take an array of remainder i. rem[20];
- Let ,Initial elements in rem[] be Zero
- In each iteration Store Remainder in the Array & Divide original number by 16
- Also Count the number of remainders stored
- To Convert into Hexadecimal Just Reverse Remainder array
- If the Remainder Array Element is greater than 10 than Use Following Table
Remainder | Display this |
10 | A |
11 | B |
12 | C |
13 | D |
14 | E |
15 | F |
Program :
#include<stdio.h> #include<conio.h> #include<math.h> void dec_hex(long int num) // Function Definition { long int rem[50],i=0,length=0; while(num>0) { rem[i]=num%16; num=num/16; i++; length++; } printf("Hexadecimal number : "); for(i=length-1;i>=0;i--) { switch(rem[i]) { 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("%ld",rem[i]); } } } //================================================ void main() { long int num; clrscr(); printf("Enter the decimal number : "); scanf("%ld",&num); dec_hex(num); // Calling function getch(); }
Output :
Enter the decimal number : 87274 Hexadecimal number : 154EA