Ask Question
17 March, 04:25

write a c+ + program that reads from the user a positive integer (in a decimal representation), and prints its binary (base 2) representation. Your program should interact with the user exactly as it shows in the following example: Enter decimal number: 76 The binary representation of 76 is 1001100

+3
Answers (1)
  1. 17 March, 07:43
    0
    The program to this question can be described as follows:

    Program:

    #include / /defining header file

    using namespace std;

    int main () / /defining main method

    {

    int x1, rem, i=1; / /defining integer variable

    long Num=0; / / defining long variable

    cout << "Input a decimal number: "; / / print message

    cin >> x1; / /input value by user

    while (x1!=0) / /loop for calculate binary number

    {

    //calculating binary value

    rem = x1%2;

    x1=x1/2;

    Num = Num + rem*i; //holding calculate value

    i=i*10;

    }

    cout <
    return 0;

    }

    Output:

    Input a decimal number: 76

    1001100

    Explanation:

    In the above code, four variable "x1, rem, i, and Num" is declared, in which three "x1, rem, and i" is an integer variable, and one "Num" is a long variable.

    In the x1 variable, we take input by the user and use the while loop to calculate its binary number. In the loop, first, we check x1 variable value is not equal to 0, inside we calculate it binary number that store in long "Num" variable, after calculating its binary number the print method "cout" is used to prints its value.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “write a c+ + program that reads from the user a positive integer (in a decimal representation), and prints its binary (base 2) ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers