Write a program that calculates the circumference and area ofa Sem radius.
时间: 2024-02-17 08:00:59 浏览: 23
Sure, here's a Python program that calculates the circumference and area of a circle with a given radius:
```python
import math
radius = float(input("Enter the radius of the circle: "))
circumference = 2 * math.pi * radius
area = math.pi * radius ** 2
print("The circumference is:", circumference)
print("The area is:", area)
```
In this program, we first prompt the user to enter the radius of the circle. We then use the math module to calculate the circumference and area of the circle, using the formulas:
- Circumference = 2 * pi * radius
- Area = pi * radius^2
Finally, we print out the results to the user.
相关问题
Write a program that calculates the number of cakes needed for a given number of guests at a party, where each guest receives 1 slice of cake. The program should ask the user to enter the number of people that are attending, and the number of slices in each cake. The program will print the number of cakes needed. Note: You will need to import the math module
Sure! Here's a program that calculates the number of cakes needed for a given number of guests at a party:
```python
import math
def calculate_cakes_needed():
num_people = int(input("Enter the number of people attending the party: "))
slices_per_cake = int(input("Enter the number of slices in each cake: "))
cakes_needed = math.ceil(num_people / slices_per_cake)
print("Number of cakes needed:", cakes_needed)
calculate_cakes_needed()
```
In this program, we first import the `math` module to use the `ceil()` function, which rounds up a number to the nearest integer.
Then, we define the `calculate_cakes_needed()` function that asks the user to enter the number of people attending the party and the number of slices in each cake. We convert the user inputs to integers using `int()`.
Next, we calculate the number of cakes needed by dividing the number of people by the number of slices per cake, and rounding up using `math.ceil()`.
Finally, we print the number of cakes needed.
Note that the code assumes that each guest will only have one slice of cake.
Write a program to find the area of a triangle, given its length of each side as 3, 5, 6.
Sure, here is a Python program to find the area of a triangle with sides of length 3, 5, and 6:
```python
# import math module for square root function
import math
# define the lengths of the sides of the triangle
a = 3
b = 5
c = 6
# calculate the semi-perimeter of the triangle
s = (a + b + c) / 2
# calculate the area of the triangle using Heron's formula
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
# print the result
print("The area of the triangle is:", area)
```
This program first imports the `math` module to use the `sqrt` function. Then it defines the lengths of the sides of the triangle as variables `a`, `b`, and `c`. The program then calculates the semi-perimeter of the triangle, which is half of the sum of the lengths of the sides. Finally, it calculates the area of the triangle using Heron's formula, which involves taking the square root of a product of the semi-perimeter and the differences between the semi-perimeter and each side length. The program prints the result, which is the area of the triangle with sides of length 3, 5, and 6.