In C coding, 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 = x / 2
Note: The above algorithm outputs the 0's and 1's in reverse order.

Ex: If the input is:

6
the output is:

011
6 in binary is 110; the algorithm outputs the bits in reverse.

#include

int main(void) {

/* Type your code here. */

return 0;
}

Respuesta :

The program illustrates the use of modulo operators

The modulo operator is used to return the remainder of a division

Take for instance:

5 % 3 is 2, because the result of 5 divided by 3 is 1 remainder 2

So, the complete program, where comments are used to explain each line is as follows:

#include<iostream>

#include<string>

using namespace std;

int main(void) {

//This declares num as integer

int num;

//This declares binary as string

string binary;

//This gets input for num

cin>>num;

//The following iteration is repeated while num is greater than 0

while(num>0){

   //This gets the remainder of num divided by 2

   binary+=to_string(num%2);

   //This divides num by 2

   num/=2;

}

//The reverses the binary string

binary = string(binary.rbegin(),binary.rend());

//This prints the binary string

cout<<binary;

return 0;

}

At the end of the program, the binary string is printed

See attachment for sample run

Read more about modulo operators at:

https://brainly.com/question/17760417

Ver imagen MrRoyal