Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- char int_to_char (int n) {
- char c = '0' + n;
- return c;
- }
- string dec_to_bin (int n) {
- string s;
- if (n < 2) {
- s = int_to_char(n);
- return s;
- }
- else {
- s = int_to_char(n%2);
- return dec_to_bin(n/2) + s;
- }
- }
- string dec_to_oct (int n) {
- string s;
- if (n < 8) {
- s = int_to_char(n);
- return s;
- }
- else {
- s = int_to_char(n%8);
- return dec_to_oct(n/8) + s;
- }
- }
- char int_to_hex (int n) {
- if (n < 10) return '0' + n;
- else return 'A' + n - 10;
- }
- string dec_to_hex (int n) {
- string s;
- if (n < 16) {
- s = int_to_hex(n);
- return s;
- }
- else {
- return dec_to_hex (n/16) + int_to_hex (n%16);
- }
- }
- int main() {
- int n;
- while (cin >> n) {
- cout << n << " = " << dec_to_bin(n) << ", " << dec_to_oct(n) << ", " << dec_to_hex (n) << endl;
- }
- }
Add Comment
Please, Sign In to add comment