1.
Write a program to compute area and perimeter of rectangle, triangle, square and
circle.
Code=>
import math
def rectangle():
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
perimeter = 2 * (length + width)
print(f"Area of the rectangle: {area}")
print(f"Perimeter of the rectangle: {perimeter}")
def triangle():
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
side1 = float(input("Enter the length of the first side: "))
side2 = float(input("Enter the length of the second side: "))
side3 = float(input("Enter the length of the third side: "))
area = 0.5 * base * height
perimeter = side1 + side2 + side3
print(f"Area of the triangle: {area}")
print(f"Perimeter of the triangle: {perimeter}")
def square():
side = float(input("Enter the side length of the square: "))
area = side * side
perimeter = 4 * side
1
print(f"Area of the square: {area}")
print(f"Perimeter of the square: {perimeter}")
def circle():
radius = float(input("Enter the radius of the circle: "))
area = [Link] * radius * radius
circumference = 2 * [Link] * radius
print(f"Area of the circle: {area}")
print(f"Circumference of the circle: {circumference}")
def main():
print("Choose a shape to calculate its area and perimeter/circumference:")
print("1. Rectangle")
print("2. Triangle")
print("3. Square")
print("4. Circle")
choice = int(input("Enter your choice (1-4): "))
if choice == 1:
rectangle()
elif choice == 2:
triangle()
elif choice == 3:
square()
elif choice == 4:
circle()
else:
print("Invalid choice! Please choose a number between 1 and 4.")
if __name__ == "__main__":
main()
2
Output=>
Choose a shape to calculate its area and perimeter/circumference:
1. Rectangle
2. Triangle
3. Square
4. Circle
Enter your choice (1-4): 2
Enter the base of the triangle: 8
Enter the height of the triangle: 6
Enter the length of the first side: 5
Enter the length of the second side: 5
Enter the length of the third side: 6
Area of the triangle: 24.0
Perimeter of the triangle: 16.0
2. Write a program to swap two numbers:
a. Using third variable
b. Without using third variable.
Code=>
x = 5
y = 7
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
x, y = y, x
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)
Output=>
Before swapping:
Value of x : 5 and y : 7
After swapping:
Value of x : 7 and y : 5
3
3. Write a program to determine whether the number is positive, negative or zero.
Code=>
def check(n):
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Equal to zero")
check(5)
check(0)
check(-5)
Output=>
Positive
Equal to zero
Negative
4. Write a program to determine whether the given number is even or odd.
Code=>
x = 24
if x % 24 == 0:
print(x,"Is Even Number")
else:
print(x, "Is Odd Number")
y = 19
if y % 19 == 0:
print(y,"Is Even Number")
else:
print(y, "Is Odd Number")
4
Output=>
24 Is Even Number
19 Is Even Number
5. Write a program to compute roots of quadratic equation.
Code=>
import cmath
a = 1
b = 5
c = 6
d = (b**2) - (4*a*c)
sol1 = (-[Link](d))/(2*a)
sol2 = (-b+[Link](d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
Output=>
The solution are (-3+0j) and (-2+0j)
6. Write a program to find the number of and sum of all integer greater than 500 and less
than 1000 that are divisible by 6.
Code=>
count=0
total_sum=0
for num in range(501,1000):
if num % 6 == 0:
count += 1
total_sum += num
print("Number of integers divisible by 6 between 500 and 1000:", count)
print("Sum of these integers:", total_sum)
Output=>
5
7. Write a program that takes two integers operands and one operator(+,-,/,%,*) from the
user, perform the requested operation and output the results.
Code=>
operand1 = float(input("Enter the first operand: "))
operand2 = float(input("Enter the second operand: "))
operator = input("Enter the operator (+, -, *, /, %): ")
if operator == '+':
print("Result:", operand1 + operand2)
elif operator == '-':
print("Result:", operand1 - operand2)
elif operator == '*':
print("Result:", operand1 * operand2)
elif operator == '/':
if operand2 != 0:
print("Result:", operand1 / operand2)
else:
print("Error: Division by zero is not allowed.")
elif operator == '%':
if operand2 != 0:
print("Result:", operand1 % operand2)
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid operator. Please use +, -, *, /, or %.")
Output=>
Enter the first operand: 5
Enter the second operand: 8
Enter the operator (+, -, *, /, %): *
Result: 40.0
6
8. Given three numbers, write a program to determine whether they can form sides of an
isosceles triangle.
Code=>
def is_isosceles_triangle(a, b, c):
if a + b > c and a + c > b and b + c > a:
if a == b or b == c or a == c:
return True
else:
return False
else:
return False
a = float(input("Enter the length of side a: "))
b = float(input("Enter the length of side b: "))
c = float(input("Enter the length of side c: "))
if is_isosceles_triangle(a, b, c):
print("The sides can form an isosceles triangle.")
else:
print("The sides cannot form an isosceles triangle.")
Output=>
Enter the length of side a: 6
Enter the length of side b: 8
Enter the length of side c: 6
The sides can form an isosceles triangle.
9. Given three points A(x1, y1), B(x2, y2) and C(x3, y3), write a program to determine
whether they are collinear i.e., lie on the same line or not.
Code=>
def are_points_collinear(x1, y1, x2, y2, x3, y3):
area = 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
return area == 0
7
x1, y1 = map(float, input("Enter coordinates for point A (x1 y1): ").split())
x2, y2 = map(float, input("Enter coordinates for point B (x2 y2): ").split())
x3, y3 = map(float, input("Enter coordinates for point C (x3 y3): ").split())
if are_points_collinear(x1, y1, x2, y2, x3, y3):
print("The points are collinear.")
else:
print("The points are not collinear.")
Output=>
Enter coordinates for point A (x1 y1): 1 1
Enter coordinates for point B (x2 y2): 2 2
Enter coordinates for point C (x3 y3): 3 3
The points are collinear.
10. Write a program to determine whether a number is palindrome or not.
Code=>
def is_palindrome(number):
return str(number) == str(number)[::-1]
number = int(input("Enter a number: "))
if is_palindrome(number):
print(f"{number} is a palindrome.")
else:
print(f"{number} is not a palindrome.")
Output=>
Enter a number: 55
55 is a palindrome.
11. Write a program to determine whether a year is prime or not.
Code=>
def is_prime(year):
8
if year < 2:
return False
for i in range(2, year):
if year % i == 0:
return False
return True
year = int(input("Enter a year: "))
if is_prime(year):
print(f"{year} is a prime number.")
else:
print(f"{year} is not a prime number.")
Output=>
Enter a year: 2
2 is a prime number.
12. Write a program to enter a number and calculate the sum of its digits.
Code=>
def sum_of_digits(number):
total = 0
while number > 0:
total += number % 10
number //= 10
return total
number = int(input("Enter a number: "))
if number < 0:
number = abs(number)
print(f"The sum of the digits of the number is: {sum_of_digits(number)}")
Output=>
Enter a number: 84567
9
The sum of the digits of the number is: 30
13. Write a program to find sum of first n positive integer numbers.
Code=>
def sum_of_first_n(n):
return n * (n + 1) // 2
n = int(input("Enter a positive integer: "))
if n > 0:
print(f"The sum of the first {n} positive integers is: {sum_of_first_n(n)}")
else:
print("Please enter a positive integer.")
Output=>
Enter a positive integer: 24
The sum of the first 24 positive integers is: 300
14. Write a program to print Fibonacci series, i.e., 0 1 1 2 3 5 8 13…
Code=>
def fibonacci_series(n):
fib_sequence = []
a, b = 0, 1
for _ in range(n):
fib_sequence.append(a)
a, b = b, a + b
return fib_sequence
n = int(input("Enter the number of terms for the Fibonacci series: "))
if n > 0:
print("Fibonacci series:")
print(" ".join(map(str, fibonacci_series(n))))
else:
print("Please enter a positive integer.")
10
Output=>
Enter the number of terms for the Fibonacci series: 20
Fibonacci series:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
15. Write a program to calculate the factorial of number.
Code=>
def factorial(number):
result = 1
for i in range(1, number + 1):
result *= i
return result
number = int(input("Enter a non-negative integer: "))
if number >= 0:
print(f"The factorial of {number} is: {factorial(number)}")
else:
print("Please enter a non-negative integer.")
Output=>
Enter a non-negative integer: 12
The factorial of 12 is: 479001600
16. Write a program to determine whether a number is Armstrong?
Code=>
def is_armstrong(number):
num_str = str(number)
num_digits = len(num_str)
total = sum(int(digit) ** num_digits for digit in num_str)
11
return total == number
number = int(input("Enter a number: "))
if is_armstrong(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")
Output=>
Enter a number: 456
456 is not an Armstrong number.
17. Write a program to calculate the HCF of given number.
Code=>
def calculate_hcf(a, b):
while b:
a, b = b, a % b
return a
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
hcf = calculate_hcf(num1, num2)
print(f"The HCF of {num1} and {num2} is: {hcf}")
Output=>
Enter the first number: 19
Enter the second number: 24
The HCF of 19 and 24 is: 1
18. Write a program to read the age of 100 persons and count the numbers of persons in
the age group of 50 to 60.
Code=>
12
def count_age_group(age_list):
count = 0
for age in age_list:
if 50 <= age <= 60:
count += 1
return count
age_list = []
for i in range(100):
age = int(input(f"Enter the age of person {i+1}: "))
age_list.append(age)
count = count_age_group(age_list)
print(f"The number of persons in the age group of 50 to 60 is: {count}")
Output=>
Enter the age of person 1: 56
Enter the age of person 2: 58
Enter the age of person 3: 78
Enter the age of person 4: 45
Enter the age of person 5: 96
Enter the age of person 6: 25
Enter the age of person 7: 45
Enter the age of person 8: 35
Enter the age of person 9: 36
Enter the age of person 10: 85
Enter the age of person 11: 5
Enter the age of person 12: 45
Enter the age of person 13: 65
Enter the age of person 14: 15
Enter the age of person 15: 20
Enter the age of person 16: 21
Enter the age of person 17: 52
13
Enter the age of person 18: 30
Enter the age of person 19: 33
Enter the age of person 20: 61
Enter the age of person 21: 25
Enter the age of person 22: 56
Enter the age of person 23: 50
Enter the age of person 24: 52
Enter the age of person 25: 26
Enter the age of person 26: 22
Enter the age of person 27: 48
Enter the age of person 28: 59
Enter the age of person 29: 47
Enter the age of person 30: 56
Enter the age of person 31: 12
Enter the age of person 32: 19
Enter the age of person 33: 23
Enter the age of person 34: 56
Enter the age of person 35: 45
Enter the age of person 36: 78
Enter the age of person 37: 89
Enter the age of person 38: 56
Enter the age of person 39: 23
Enter the age of person 40: 40
Enter the age of person 41: 32
Enter the age of person 42: 54
Enter the age of person 43: 24
Enter the age of person 44: 26
Enter the age of person 45: 36
Enter the age of person 46: 75
Enter the age of person 47: 85
Enter the age of person 48: 56
Enter the age of person 49: 54
Enter the age of person 50: 52
Enter the age of person 51: 51
14
Enter the age of person 52: 24
Enter the age of person 53: 25
Enter the age of person 54: 26
Enter the age of person 55: 19
Enter the age of person 56: 18
Enter the age of person 57: 21
Enter the age of person 58: 23
Enter the age of person 59: 39
Enter the age of person 60: 68
Enter the age of person 61: 67
Enter the age of person 62: 65
Enter the age of person 63: 25
Enter the age of person 64: 50
Enter the age of person 65: 67
Enter the age of person 66: 63
Enter the age of person 67: 38
Enter the age of person 68: 71
Enter the age of person 69: 82
Enter the age of person 70: 93
Enter the age of person 71: 81
Enter the age of person 72: 73
Enter the age of person 73: 76
Enter the age of person 74: 35
Enter the age of person 75: 59
Enter the age of person 76: 88
Enter the age of person 77: 15
Enter the age of person 78: 61
Enter the age of person 79: 32
Enter the age of person 80: 85
Enter the age of person 81: 65
Enter the age of person 82: 15
Enter the age of person 83: 56
Enter the age of person 84: 26
Enter the age of person 85: 28
15
Enter the age of person 86: 29
Enter the age of person 87: 49
Enter the age of person 88: 69
Enter the age of person 89: 67
Enter the age of person 90: 35
Enter the age of person 91: 9
Enter the age of person 92: 44
Enter the age of person 93: 66
Enter the age of person 94: 32
Enter the age of person 95: 88
Enter the age of person 96: 77
Enter the age of person 97: 76
Enter the age of person 98: 33
Enter the age of person 99: 45
Enter the age of person 100: 25
The number of persons in the age group of 50 to 60 is: 18
19. Write a program that will read a positive integer and determine and print its binary
equivalent.
Code=>
def to_binary(number):
return bin(number)[2:]
number = int(input("Enter a positive integer: "))
if number >= 0:
print(f"The binary equivalent of {number} is: {to_binary(number)}")
else:
print("Please enter a positive integer.")
Output=>
Enter a positive integer: 56
The binary equivalent of 56 is: 111000
16
20. Write a program to calculate pow(x,n), i.e., to calculate xn.
Code=>
def power(x, n):
return x ** n
x = float(input("Enter the base number (x): "))
n = int(input("Enter the exponent (n): "))
result = power(x, n)
print(f"{x} raised to the power {n} is: {result}")
Output=>
Enter the base number (x): 4
Enter the exponent (n): 2
4.0 raised to the power 2 is: 16.0
17