1. Program to find square of a number.
# Function to calculate the square of a number
def calculate_square(number):
return number ** 2
# Ask the user for a number
user_input = float(input("Enter a number: "))
# Call the function to calculate the square
result = calculate_square(user_input)
# Display the result
print("The square of the", user_input, "is", result)
2. Program to Check if a Number is Even or Odd
def is_even(number):
return number % 2 == 0
user_input = int(input("Enter a number: "))
if is_even(user_input):
print("The number", user_input, "is even.")
else:
print("The number", user_input, "is odd.")
3. Program to Calculate the Area of a Circle
def calculate_area(radius):
pi = 3.14159
return pi * radius ** 2
radius = float(input("Enter the radius of the circle: "))
area = calculate_area(radius)
print("The area of a circle with radius", radius, "is", area)
4. Program to Find the Maximum of Three Numbers
def find_maximum(a, b, c):
return max(a, b, c)
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
maximum = find_maximum(num1, num2, num3)
print("The maximum of", num1, ",", num2, "and", num3, "is", maximum)
5. Program to Convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
celsius = float(input("Enter the temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print("The temperature of", celsius, "degrees Celsius is", fahrenheit, "degrees
Fahrenheit.")
6. Program to Calculate the Factorial of a Number
def factorial(number):
result = 1
for i in range(1, number + 1):
result *= i
return result
user_input = int(input("Enter a number: "))
result = factorial(user_input)
print("The factorial of", user_input, "is", result)
7. Program to Check if a Number is Prime
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
user_input = int(input("Enter a number: "))
if is_prime(user_input):
print("The number", user_input, "is a prime number.")
else:
print("The number", user_input, "is not a prime number.")
8. Program to Reverse a String
def reverse_string(text):
return text[::-1]
user_input = input("Enter a string: ")
reversed_text = reverse_string(user_input)
print("The reverse of the string", user_input, "is", reversed_text)
9. Program to Check if a String is a Palindrome
def is_palindrome(text):
return text == text[::-1]
user_input = input("Enter a string: ")
if is_palindrome(user_input):
print("The string", user_input, "is a palindrome.")
else:
print("The string", user_input, "is not a palindrome.")
10. Program to Find the Sum of a List of Numbers
def sum_of_list(numbers):
return sum(numbers)
user_input = input("Enter numbers separated by spaces: ")
numbers = list(map(float, user_input.split()))
total = sum_of_list(numbers)
print("The sum of the numbers", numbers, "is", total)