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 modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.LABACTIVITY7.14.1: LAB: Convert to binary

Respuesta :

Answer:

// Java Code

import java.io.*;  

class Binary {  

//function to convert integer to binary  

static void convert(int num)  

{  

 // array to store 32 bit binary number  

 int bin[] = new int[32];  

 int i=0,j;  

 while (num > 0) {  

  // store the remainder in the array  

  bin[i] = num % 2;  

  num = num / 2;  

  i++;  

 }    

 System.out.println("Number in Binary is");

 // Loop to print the array in reverse order

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

  System.out.print(bin[j]);  

}  

 // main method  

public static void main(String[] args)throws IOException

{  

    //Using BufferedReader class for reading input

    InputStreamReader x = new InputStreamReader(System.in);

    BufferedReader inp = new BufferedReader(x);

 int n;

 System.out.println("Enter the decimal number to convert to binary");

 n=Integer.parseInt(inp.readLine());

 convert(n);  

}  

}

Explanation:

Read an input from the user. Declare an array of size 32 to store 32 bits binary numbers. Keep dividing the input by 2 and store the remainder in the array until the number is no more divisible by 2. Now print the remainders stored in the array in reverse order.

Output:

Enter the decimal number to convert to binary

6

Number in Binary is

110

#include <stdio.h>

#include <stdlib.h>

int main ()

{

   int n, i = 1, k;

   int j;

   binary int [10];

   printf ("Enter Number \ t:");

   scanf ("% d", & n);

   while (n> 0) {

       binary [i] = n% 2;

        n = n / 2;

        i = i + 1;

        k = i;

   }

   for (j = k-1; j> 0; j--)

       {

       printf ("% d", binary [j]);

   }

}

Further explanation

Basically, what is meant by number conversion is the process of changing the form of one number to another number that still has the same value. Converting decimal numbers to binary numbers means changing the form of decimal numbers to form binary numbers that still have the same value.

The method of converting decimal numbers to binary numbers is quite easy, by dividing decimal numbers into a base of binary numbers that is 2, the results are then rounded down and the remainder of the division results are stored or recorded. Make rounding down until the value reaches zero. The remaining shares are then sorted from the last to the earliest. The rest of the sorted order is the result of the conversion of decimal numbers to binary numbers.

Example of Converting Decimal Numbers to Binary Numbers

Convert a decimal number value 50 to a binary number:

50/2 = 25 the remainder is 0

25/2 = 12 remainder is 1

12/2 = 6 the remainder is 0

6/2 = 3 remainder is 0

3/2 = 1 remainder is 1

1/2 = 0 the remainder is 1

Learn More

Simple modulo calculation program  https://brainly.com/question/13572257

C ++ programming language  https://brainly.com/question/13572257

Details

Class: College

Subject: Computers and Technology

Keywords: program, modulo, c++

ACCESS MORE