The formula E = states that the equivalent energy (E) can be calculated as the mass (m) multiplied by the speed of light (c = about 10^{8}$$ m/s) squared. Write a program that accepts the mass of an object and determines its energy.
Program to Calculate Energy using E = mc²
python
Program to calculate energy using Einstein's formula E = mc²
Define the speed of light
c = 3 * 10 ** 8 # Speed of light in m/s
Accept mass from user
mass = input("Enter the mass of the object (in kg): ") mass = float(mass) # Convert string input to float
Calculate energy using the formula E = mc²
energy = mass * c ** 2
Display the result
print("The equivalent energy is:", energy, "joules")
Explanation:
The program begins by defining the speed of light as a variable c with the value 3 * 10 ** 8 m/s. The ** operator is used for exponentiation in Python.
The program then accepts the mass of the object from the user using the input() function. Since the input() function returns a string by default, we use explicit conversion (type casting) to convert it to a float using float() function. This ensures proper arithmetic operations.
The energy is calculated using the formula E = mc², which translates to energy = mass * c ** 2 in Python. The ** operator squares the value of c before multiplication.
Finally, the result is displayed using the print() statement, showing the calculated energy in joules.
Explanation
This question tests the student's understanding of arithmetic operators, input handling, and type conversion in Python. The textbook context shows that input returns a string which must be converted using int() or float() for mathematical calculations. The ** operator is used for exponentiation as shown in the examples.
Solution Steps
Step 1: Define speed of light c = 10^{8}$$ m/s using exponentiation operator **
Step 2: Accept mass from user using input() function
Step 3: Convert string input to float using float() for numerical calculation
Step 4: Calculate energy using formula E = m × c²
Step 5: Display the result using print() statement