Python Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Write programs of the following in python:

1. Calculate Area of a Rectangle:


length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area of the rectangle is:", area)

2. Calculate BMI (Body Mass Index):


weight = float(input("Enter weight (in kg): "))
height = float(input("Enter height (in meters): "))
bmi = weight / (height ** 2)
print("BMI is:", bmi)

3. Calculate Simple Interest:


principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time (in years): "))
simple_interest = (principal * rate * time) / 100
print("Simple Interest is:", simple_interest)

4. Convert Celsius to Fahrenheit:


celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit is:", fahrenheit)

5. Swap Two Numbers:


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("Swapped numbers: a =", a, "b =", b)

6. Check Even or Odd:


number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")

7. Check Positive or Negative:


number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")

8. Calculate Factorial:
n = int(input("Enter a number: "))
factorial = 1
for i in range(1, n + 1):
factorial *= i
print("Factorial is:", factorial)

9. Check Leap Year:


year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year.")
else:
print("Not a leap year.")

10. Find Maximum of Three Numbers:


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
max_num = max(a, b, c)
print("Maximum number is:", max_num)

11. Calculate Power:


base = float(input("Enter the base: "))
exponent = float(input("Enter the exponent: "))
result = base ** exponent print("Result is:", result)

12. Simple Calculator:


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operation = input("Enter the operation (+, -, *, /): ")

if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2
elif operation == '*':
result = num1 * num2
elif operation == '/':
if num2 == 0:
result = "Cannot divide by zero"
else:
result = num1 / num2
else:
result = "Invalid operation"

print("Result is:", result)


```
13. Determine the Smallest Element in a List:
numbers = [5, 3, 8, 1, 6]
smallest = min(numbers)
print("The smallest number is:", smallest)
14. Determine the Largest Element in a List:
numbers = [5, 3, 8, 1, 6]
largest = max(numbers)
print("The largest number is:", largest)

15. Check for Prime Number:


num = int(input("Enter a number: "))
is_prime = True if num <= 1:
is_prime = False
else: for i in range(2, int(num**0.5) + 1): if num % i == 0: is_prime = False break if is_prime:
print("It's a prime number.") else: print("It's not a prime number.")

16. Reverse a String:


text = input("Enter a string: ")
reversed_text = text[::-1]
print("Reversed string:", reversed_text)

17. Check for Palindrome:


text = input("Enter a string: ") is_palindrome = text == text[::-1] if is_palindrome: print("It's a
palindrome.") else: print("It's not a palindrome.")

18. Count Vowels and Consonants:


text = input("Enter a string: ") text = text.lower() # Convert text to lowercase vowels = 'aeiou'
num_vowels = sum(1 for char in text if char in vowels) num_consonants = len(text) -
num_vowels print("Vowels:", num_vowels) print("Consonants:", num_consonants)

19. Generate Fibonacci Sequence:


n = int(input("Enter the number of terms: ")) a, b = 0, 1 for _ in range(n): print(a, end=' ') a, b = b,
a+b

20. Find the Median of a List:


numbers = [5, 3, 8, 1, 6, 2, 7, 4] sorted_numbers = sorted(numbers) n = len(sorted_numbers) if n
% 2 == 0: median = (sorted_numbers[n // 2 - 1] + sorted_numbers[n // 2]) / 2 else: median =
sorted_numbers[n // 2] print("Median:", median)

You might also like