-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
decimal_to_hexadecimal.cpp
59 lines (52 loc) · 1.07 KB
/
decimal_to_hexadecimal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
//CONVERSION OF DECIMAL NUMBER TO HEXADECIMAL NUMBER
void decimal_to_hexadecimal(int n)
{
int i=0;
string ans="";
int last_digit=0;
char x;
while(n)
{
last_digit=n%16;
n=n/16;
/* Condition checking :
If digit greater than 9, X will be alphabet
Else X will be a number */
if(last_digit>9)
x=last_digit+'A'-10;
else
x=last_digit+'0';
ans[i]=x;
i++;
}
//Loop for printing reverse String
cout<<"\n The Required Hexadecimal Number is::";
for(int k=i-1;k>=0;k--)
cout<<ans[k];
}
int main()
{
int decimal;
cout<<"\n ENTER THE DECIMAL NUMBER::";
cin>>decimal;
decimal_to_hexadecimal(decimal);
return 0;
}
/* Testcases:
INPUT-
ENTER THE DECIMAL NUMBER::463
OUTPUT-
The Required Hexadecimal Number is::1CF
INPUT-
ENTER THE DECIMAL NUMBER::100
OUTPUT-
The Required Hexadecimal Number is::64
INPUT-
ENTER THE DECIMAL NUMBER::516042
OUTPUT-
The Required Hexadecimal Number is::7DFCA
Time Complexity- O(n);
Space Complexity- O(1);
*/