answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nataly
2 months ago
9

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 binar

y. 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
Computers and Technology
2 answers:
maria [1K]2 months ago
5 0

#include <stdio.h>

#include <stdlib.h>

int main ()

{

   int n, i = 1, k;

   int j;

   int binary[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

Converting numbers means changing a number into a different format that keeps the same value. Transforming decimal to binary means changing the decimal digits into binary digits of identical value.

The process involves dividing the decimal number by 2, recording the remainder each time while rounding down until zero is reached. The remainders are then read backwards to produce the binary number.

For example, converting 50 decimal to binary:

50/2=25 remainder 0

25/2=12 remainder 1

12/2=6 remainder 0

6/2=3 remainder 0

3/2=1 remainder 1

1/2=0 remainder 1

Harlamova29_29 [1K]2 months ago
4 0

Answer:

// Java Code

import java.io.*;

class Binary {

// method to convert integer to binary

static void convert(int num) {

int bin[] = new int[32];

int i=0,j;

while (num > 0) {

bin[i] = num % 2;

num = num / 2;

i++;

}

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

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

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

}

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

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:

The program reads an input, creates an array for 32-bit binary numbers, repeatedly divides the input by 2 storing remainders, then prints the remainders in reverse order.

Output:

Enter the decimal number to convert to binary

6

Number in Binary is

110

You might be interested in
In mathematics, the factorial of a positive integer n, denoted as n! , is the product of all positive integers less than or equa
oksian1 [950]
public static int factorial(int n) { if (n >= 1 && n <=12) { if (n == 1) return 1; else return n * factorial(n - 1); } else return -1; } Explanation: The factorial method takes a single integer n as input. It checks if n is within the range of 1 to 12. If it is, it further checks if n equals 1. If it is indeed 1, it returns 1 as the factorial. Otherwise, it recursively calls itself with n decreased by 1, multiplying the result by n. If n is outside this range, the method returns -1 indicating the input is invalid.
6 0
1 month ago
Read 2 more answers
When handling project scope creep, which are two things that all parties involved need to be aware of?
zubka84 [1067]

Additional resources required for the projects

Added time necessary for the project

Clarification:

In any project management scenario, there will naturally be unexpected changes and additional needs, hence to successfully complete a project, one must allocate more time and resources. It is advisable that, based on the project specifics, the end user should maintain a sufficient buffer to accommodate any variations in human resources and the extra time necessary for project completion.

When planning the project, a consideration of extra time per each task is essential.

Every task within project management is categorized under distinct scopes of work.

3 0
1 month ago
George and Miguel are considering opening up a shoe store but first need to do market research. Which one of these is NOT part o
Natasha_Volkova [1026]
The answer is D. Based on their research findings, they will draw conclusions and make business decisions.
7 0
2 months ago
Read 2 more answers
Which kind of file would be hurt most by lossy compression algorithm
oksian1 [950]

Response: a file containing audio

Clarification:

8 0
1 month ago
Read 2 more answers
Write an expression to print each price in stock_prices. Sample output with inputs: 34.62 76.30 85.05
8_murik_8 [964]

Answer:

Below is the Python code:

stock_prices = input().split() #this takes input and separates it into a list

for price in stock_prices: #this loops through each stock price

   print("$",price) #this outputs each stock price prefixed by a dollar sign

Explanation:

The logic behind the program is clearly outlined in the attached comments. To illustrate the program's workings, let's consider an example:

Imagine the user inputs the stock_prices values of

34.62 76.30 85.05

The input() method captures user input, while the split() method divides the input string, providing a list of strings as:

['34.62', '76.30', '85.05']

Following that, the loop statement for price in stock_prices: iterates through each item in the list, and print("$",price) displays each value from the list on the output screen with a dollar sign, as follows:

$ 34.62                                                                                                            

$ 76.30                                                                                                            

$ 85.05    

4 0
2 months ago
Other questions:
  • Explain working principle of computer?​
    13·1 answer
  • Strlen("seven"); what is the output?
    14·1 answer
  • What are the differences between a policy, a standard, and a practice? What are the three types of security policies? Where woul
    15·1 answer
  • A technician with a PC is using multiple applications while connected to the Internet. How is the PC able to keep track of the d
    8·2 answers
  • Which decimal value (base 10) is equal to the binary number 1012?
    5·1 answer
  • Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 3
    15·1 answer
  • A 1.17 g sample of an alkane hydrocarbon gas occupies a volume of 674 mL at 28°C and 741 mmHg. Alkanes are known to have the gen
    14·1 answer
  • Suppose a linked list of 20 nodes. The middle node has a data –250. Write the pseudocode to replace the middle node of the linke
    6·2 answers
  • This question refers to a standard deck of playing cards. If you are unfamiliar with playing cards, there is an explanation in P
    10·2 answers
  • Cindy visits her favorite website on a lunch break using a hospital computer. After she downloads a file, she notices that the c
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!