Skip to content

Commit 673f3a3

Browse files
Create BaseSystem.cpp
1 parent b057d0c commit 673f3a3

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

‎BaseSystem.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Randall Hall
3+
C++ program to convert an integer in base 10 to a defined base
4+
*/
5+
6+
#include <iostream>
7+
using namespace std;
8+
9+
void decToBin (int, int);
10+
int main()
11+
12+
{
13+
int decimal;
14+
int base;
15+
16+
cout << "Enter a non-negative base ten integer to convert to a different base: ";
17+
cin >> decimal;
18+
cout << "Convert to what base? ";
19+
cin >> base;
20+
21+
do
22+
{
23+
if (decimal >= 0)
24+
{
25+
cout << "" << decimal << " (base 10) = ";
26+
decToBin (decimal, base);
27+
cout << " (base " << base << "). " << endl;
28+
}
29+
else
30+
cout << "" << decimal << " is not a non-negative integer. " << endl;
31+
} while (cin >> decimal);
32+
return 0;
33+
}
34+
35+
void decToBin(int num, int base)
36+
37+
{
38+
if (num > 0)
39+
{
40+
decToBin (num/base, base);
41+
cout << num % base;
42+
}
43+
else
44+
cout << "0";
45+
}

0 commit comments

Comments
 (0)