1answer.
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
iren2701 [21]
3 years ago
12

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 2
Computers and Technology
1 answer:
Mashcka [7]3 years ago
6 0

Answer:

Following are the code to this question:

def binarynumber(num):#defining a method binarynumber that accepts a parameter num

   x=""#defining a string variable x

   if (num!=0):#defining if condition to check number not equal to 0

       while (num>=1):#defining a loop that check value is grater then equal to one

           if (num %2==0):#defining if condition to check num is even

               x=x+"0" #add string value 0 in num variable

               num=num/2 #divide the value by 2

           else:#defining else block

               x=x+"1"#add string value 1 in num variable

               num=(num-1)/2#first subtract 1 into num variable then divide the value by 2

   else:

       x="0"#assign string value 0 in num variable  

   return "".join(reversed(x))#return value

num = int (input ("Enter any number: "))#defining num variable that input the integer value

print (binarynumber(num))#using print method to call method binarynumber with passing num parameter

Output:

Enter any number: 12

1100

Explanation:

  • In the above python code a method "binarynumber" is declared, in which the "num" variable passes as the parameter inside the method a string variable "x" is declared that stores all converted values.
  • Inside the method and if the block is declared that checks number value is not equal to 0 if this condition is false then it will add string value and reverse its value.
  • Or if the condition is true it defines a while loop that calculates the given number binary digits and returns its value.
  • At the last step, the num variable is declared that inputs the integer value from the user end and calls the method by using the print method.    
You might be interested in
An introduction to object-oriented programming.
Scorpion4ik [409]

Answer:

The csharp program is given below.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

   public partial class Form1 : Form

   {

       public Form1()

       {

           InitializeComponent();

      }

       private void button6_Click(object sender, EventArgs e)

       {

           int total = (Convert.ToInt32(textBox1.Text) +  Convert.ToInt32(textBox2.Text) + Convert.ToInt32(textBox3.Text) + Convert.ToInt32(textBox4.Text) + Convert.ToInt32(textBox5.Text));

           int dozen = total / 12;

           int eggs = total % 12;

           textBox6.Text = dozen.ToString();

           textBox7.Text = eggs.ToString();

       }

       private void button7_Click(object sender, EventArgs e)

       {

           textBox1.Text = "";

           textBox2.Text = "";

           textBox3.Text = "";

           textBox4.Text = "";

           textBox5.Text = "";

           textBox6.Text = "";

           textBox7.Text = "";

       }

        private void button8_Click(object sender, EventArgs e)

       {

           Close();

       }

    }

}

Explanation:

1. The integer variables are declared for total eggs, number of dozens and number of eggs.

2. The input of each text box is converted into integer by using the Convert.ToInt32() method on the value of that particular text box.

3. All the inputs are added and the sum is assigned to variable, total.

4. The number of dozens are obtained by dividing total by 12 and assigning the value to the variable, dozen.

5. The number of extra eggs are obtained by taking the modulus of total and 12 and the value is assigned to the variable, eggs.

6. The integer values in the variables, dozen and eggs, are converted into string using the ToString() function with that particular value.

dozen.ToString();

eggs.ToString();

7. The text boxes are assigned the respective values of dozens and number of eggs.

textBox6.Text = dozen.ToString();

textBox7.Text = eggs.ToString();

8. Two additional buttons, clear and exit, are also added.

9. The clear button erases the contents of all the text boxes. The Text property of each textbox is set to “” thereby clearing all the text fields.

10. The exit button closes the application using Close() function.

11. The program is made in visual studio and the output is attached.

3 0
3 years ago
Write a SELECT statement that returns four columns from the Invoices table, named Number, Total, Credits, and Balance: Number Co
AleksAgata [21]

Answer and Explanation:

For returning four columns from the  Invoices table, named Number, Total, Credits, and Balance the SELECt statement is as follows

Select     Invoice Number     As     Number

              Invoice Total          As      Total

Payment total + credit total  As      Credits

Invoice total

(Payment total + credit total) As     Balance

These are displayed as above

7 0
3 years ago
Which one of the following devices would you choose to meet those requirements?
tensa zangetsu [6.8K]
What are the requirements
5 0
1 year ago
Read 2 more answers
Most wheel/tire assemblies are balanced using lead weights which should be removed and segregated from other metals
crimeas [40]
The answer is true. I took the test.
6 0
2 years ago
Read 2 more answers
Cybercrime has a serious economic impact on national and global economies that costs the _____ of its GDP every year.
maks197457 [2]

Answer:

World at least 1 percent

Explanation:

Cybercrime are crimes that focuses on networks of computers whereby a computer is used to commit the crime or a computer is the target of the crime, with the possible effect of a threat to the security and financial wellbeing of a person, an establishment or country

According to the report by Zhanna Malekos Smith and Eugenia Lostri, 'The Hidden Costs of Cybercrime' the monetary cost of cybercrime which is estimated at $945 billion is <u>a little above 1% of the World's GDP</u> which is around $87.55 trillion.

4 0
2 years ago
Other questions:
  • What are the 6 external parts of a computer
    14·1 answer
  • The structure and organization of data in a database is called a data ____.
    11·1 answer
  • What is an enterprise system
    14·2 answers
  • Match each task with the features necessary to complete the task
    10·1 answer
  • A financially stable person is able to
    12·2 answers
  • What will be displayed as a result of executing the following code?
    5·1 answer
  • Which of the following would you not see on a windows 10 start menu?
    6·1 answer
  • Which of the following is an easy steps to take to avoid ESSD well working on your computer?
    5·1 answer
  • _____moves the head to proper track
    13·2 answers
  • Hewo want to talk! #bored<br>Will randomly mark brainlest
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!