Decimal Number System: It is also called base 10 system or Denary system. It refer to any number written in decimal notation format.
Hexadecimal Number System: It is also called base 16 system or Hex system. It refer to any number written in hexadecimal notation format. It uses sixteen different symbols 0-9 and A, B, C, D, E, F represent 10-15.
Algorithm:
- Divide the decimal number by 16. Consider the division operation as an integer division.
- Note down the remainder (in hexadecimal).
- Divide the result again by 16 and again consider the division as an integer division.
- Repeat step 2 and 3 until result is 0.
- The hex value is the digit sequence of the remainders from the last to first
#include "iostream" #include "conio.h" #include "math.h" using namespace std; void dtoh(int d); int main() { int d; cout<< "Enter a decimal number system: "; cin >> d ; dtoh(d); return (0); } void dtoh(int d) { int b,c=0,a[5],i=0; b=d; while (b>15) { a[i]=b%16; b=b/16; i++; c++; } a[i]=b; cout << "Its hexadecimal equivalent is : "; for (i=c;i>=0;--i) { if (a[i]==10) cout << "A"; else if (a[i]==11) cout << "B"; else if (a[i]==12) cout << "C"; else if (a[i]==13) cout << "D"; else if (a[i]==14) cout << "E"; else if (a[i]==15) cout << "F"; else cout << a[i]; } }
Input: Decimal number : 12
Output: Hexadecimal number: C
Input: Decimal number : 4023
Output: Hexadecimal number: FB7
No comments:
Post a Comment