Welcome to Westonci.ca, where finding answers to your questions is made simple by our community of experts. Join our platform to connect with experts ready to provide precise answers to your questions in different areas. Join our Q&A platform to connect with experts dedicated to providing accurate answers to your questions in various fields.

Write a program that declares and initializes a variable representing the weight in milligrams from the keyboard. The program displays the equivalent weight in kilograms, grams, and milligrams. For example, 1050042 milligrams are equivalent to 1 kilogram, 50 grams, and 42 milligrams.

Sagot :

Answer:

weight = int(input("Enter weight in milligrams: "))

kilograms = int(weight / 1000000)

grams = int((weight - (kilograms * 1000000)) / 1000)

milligrams = weight - ((kilograms * 1000000) + (grams * 1000))

print("{} milligrams are equivalent to {} kilogram(s), {} gram(s), and {} milligram(s)".format(weight, kilograms, grams, milligrams))

Explanation:

*The code is in Python.

Ask the user to enter the weight and set it to the variable weight

Calculate the kilograms, divide the weight by 1000000 and cast the result to the int (If the weight is 1050042, kilograms would be 1050042/1000000 = 1)

Calculate the grams, subtract the kilograms from the weight, divide it by 1000 and cast the result to the int (If the weight is 1050042, grams would be int((1050042 - (1 * 1000000)) / 1000) = 50)

Calculate the milligrams, subtract the kilograms and grams from the weight (If the weight is 1050042, milligrams would be 1050042 - ((1 * 1000000) + (50 * 1000)) = 42)

Print the weight, kilograms, grams, and milligrams in the required format

In this exercise we have to use the knowledge of the python language to write the code, so we have to:

The code is in the attached photo.

Some important information informed in the statement that we have to use in the code is:

  • Calculate the kilograms, divide the weight by 1000000 and cast the result.
  • Calculate the grams, subtract the kilograms from the weight, divide it by 1000 and cast the result.
  • Calculate the milligrams, subtract the kilograms and grams from the weight.

So to make it easier the code can be found at:

weight = int(input("Enter weight in milligrams: "))

kilograms = int(weight / 1000000)

grams = int((weight - (kilograms * 1000000)) / 1000)

milligrams = weight - ((kilograms * 1000000) + (grams * 1000))

print("{} milligrams are equivalent to {} kilogram(s), {} gram(s), and {} milligram(s)".format(weight, kilograms, grams, milligrams))

See more about python at brainly.com/question/26104476

View image lhmarianateixeira