8.10 LAB: Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x

Respuesta :

Answer:

// This program is written in C++ programming language

// Comments are used for explanatory purpose

// Program starts here

#include <iostream>

#include <string>

using namespace std;

// Declare variables

int inputvar;

// Declare output variable as array

int outputvar[32];

// Set a counter for binary array

int i = 0;

while (inputvar > 0) {

// Divide inputvar by 2 and store remainder in outputvar

outputvar[i] = n % 2;

inputvar/=2;

i++; // increment i by 1

}

// End of division

// Prin resulting array in reverse order

for (int j = i - 1; j >= 0; j--) {

cout << outputvar[j];

}

return 0;

}

// End of Program