-
Notifications
You must be signed in to change notification settings - Fork 0
/
Again.cpp
56 lines (47 loc) · 1.14 KB
/
Again.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
// C++ program to convert hexadecimal to decimal
#include<iostream>
#include<string.h>
using namespace std;
// Function to convert hexadecimal to decimal
int hexadecimalToDecimal(string hexVal)
{
int len = hexVal.size();
// Initializing base value to 1, i.e 16^0
int base = 1;
int dec_val = 0;
// Extracting characters as digits from last character
for (int i=len-1; i>=0; i--)
{
// if character lies in '0'-'9', converting
// it to integral 0-9 by subtracting 48 from
// ASCII value.
if (hexVal[i]>='0' && hexVal[i]<='9')
{
dec_val += (hexVal[i] - 48)*base;
// incrementing base by power
base = base * 16;
}
// if character lies in 'A'-'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal[i]>='A' && hexVal[i]<='F')
{
dec_val += (hexVal[i] - 55)*base;
// incrementing base by power
base = base*16;
}
}
return dec_val;
}
// Driver program to test above function
int main()
{
string hexNum;
cin>>hexNum;
int a= hexadecimalToDecimal(hexNum);
if(a&1)
cout<<1<<endl;
else
cout<<0<<endl;
return 0;
}