Question #43589

Write a program that prints a table of binary, octal and hexadecimal equivalents of the decimal using do-while statement. Apply the same range of number used in question 2. [Hint: you can use the stream manipulators dec, oct and hex to display integers in decimal, octal and hexadecimal formats, respectively.
Copy / print screen both code and output results
Draw the flowchart for coding

Expert's answer

Answer on Question #43589, Programming, C++

Problem.

Write a program that prints a table of binary, octal and hexadecimal equivalents of the decimal using do-while statement. Apply the same range of number used in question 2. [Hint: you can use the stream manipulators dec, oct and hex to display integers in decimal, octal and hexadecimal formats, respectively. Copy / print screen both code and output results Draw the flowchart for coding.

**Remark.** The range of number isn't mentioned in the problem. We suppose that the input number is a positive integer number,

Solution.

Code

#include <iostream>
#include <string>
using namespace std;
int main() {
    int n;
    string s = "";
    int r;
    cout << "Enter the nonegative integer number: ";
    cin >> n;
    int m = n;
    // The binary representation loop
    if (m < 2) {
        s.insert(0, to_string(m));
    }
    else {
        do {
            r = m % 2;
            m = m / 2;
            s.insert(0, to_string(r));
            if (m < 2) {
                s.insert(0, to_string(m));
            }
        } while (m >= 2);
    }
    cout << "The binary representation: " << s << endl;
    cout << "The octal representation: " << oct << n << endl;
    cout << "The decimal representation: " << dec << n << endl;
    cout << "The hexadecimal representation: " << hex << n << endl;
    return 0;
}

Flowchart</string></iostream>

cout << "The binary representation:" << s << endl;
cout << "The octal representation:" << oct << n << endl;
cout << "The decimal representation:" << dec << n << endl;
cout << "The hexadecimal representation:" << hex << n << endl;


FINISH

Result


LATEST TUTORIALS
APPROVED BY CLIENTS