forked from cpp-tutor/learnmoderncpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-base-n.cpp
More file actions
29 lines (25 loc) · 655 Bytes
/
Copy path04-base-n.cpp
File metadata and controls
29 lines (25 loc) · 655 Bytes
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
// 04-base-n.cpp : print out a number to given base
import std;
using namespace std;
void print_base_n(unsigned long long num, unsigned base = 10);
int main() {
cout << "Please enter a number (in decimal): ";
long long n{};
cin >> n;
cout << "Please enter the required base (2-16): ";
int b{};
cin >> b;
if ((b >= 2) and (b <= 16)) {
print_base_n(n, b);
cout << '\n';
}
else {
cerr << "Base not in range.\n";
}
}
void print_base_n(unsigned long long num, unsigned base) {
if (num >= base) {
print_base_n(num / base, base);
}
cout << "0123456789abcdef"[num % base];
}