212k views
3 votes
zybooks cis 110 challenge activity 9.8.1 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. Your program should define and call a function: Function Integer To Binary(integer num) returns nothing The function should output 1's and 0's representing the integer in binary (in reverse).

User Panu Logic
by
5.5k points

1 Answer

1 vote

Answer:

public class IntegerToBinary

{

public static void main(String[] args) {

integerToBinary(6);

}

public static void integerToBinary(int num){

while(num > 0){

System.out.print(num%2);

num = Math.floorDiv(num, 2);

}

}

}

Step-by-step explanation:

*The code is in Java.

Create a function called integerToBinary that takes one parameter, num

Inside the function, create a while loop that iterates while the num is greater than 0. Inside the loop, print the num%2. Then, get the floor division of the num by to and assign it to the num.

Inside the main, call the function with parameter 6.

User Charles Ju
by
4.7k points