Uploaded by yo yo

Week 10 Advanced Programming Practices

advertisement
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
FACULTY OF ENGINEERING AND TECHNOLOGY
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTATIONAL INTELLIGENCE
21CSC203P ADVANCED PROGRAMMING PRACTICE
WEEK 10
Karan Pillai
RA2212701010013
1)def calculate_sum_two_numbers(num1, num2):
return num1 + num2
def calculate_sum_three_numbers(num1, num2, num3):
return num1 + num2 + num3
def calculate_final_sum(sum_result):
if 120 <= sum_result <= 320:
return 200
else:
return sum_result
num1 = 50
num2 = 70
sum_two_numbers = calculate_sum_two_numbers(num1, num2)
final_sum_two_numbers = calculate_final_sum(sum_two_numbers)
print(f"Sum of two numbers: {final_sum_two_numbers}")
num1 = 100
num2 = 120
num3 = 80
sum_three_numbers = calculate_sum_three_numbers(num1, num2, num3)
final_sum_three_numbers = calculate_final_sum(sum_three_numbers)
print(f"Sum of three numbers: {final_sum_three_numbers}")
2)def find_maximum_of_three_numbers(num1, num2, num3):
max_num = max(num1, num2, num3)
return max_num
num1 = 10
num2 = 20
num3 = 15
max_number = find_maximum_of_three_numbers(num1, num2, num3)
print(f"The maximum of {num1}, {num2}, and {num3} is: {max_number}")
3)import math
def is_even(num):
return num % 2 == 0
def is_odd(num):
return not is_even(num)
def is_prime(num):
if num < 2:
return False
if num == 2:
return True
if is_even(num):
return False
max_divisor = math.isqrt(num) + 1
for i in range(3, max_divisor, 2):
if num % i == 0:
return False
return True
number = int(input("Enter a number: "))
if is_even(number):
print(f"{number} is even.")
else:
print(f"{number} is odd.")
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
4)def reverse_string(input_str):
return input_str[::-1]
def is_palindrome(input_str):
reversed_str = reverse_string(input_str)
return input_str == reversed_str
input_str = input("Enter a string: ")
reversed_str = reverse_string(input_str)
print(f"Reversed string: {reversed_str}")
if is_palindrome(input_str):
print(f"{input_str} is a palindrome.")
else:
print(f"{input_str} is not a palindrome.")
5)def generate_fibonacci_sequence(num_terms):
fibonacci_sequence = []
if num_terms == 0:
return fibonacci_sequence
a, b = 0, 1
fibonacci_sequence.append(a)
if num_terms == 1:
return fibonacci_sequence
fibonacci_sequence.append(b)
for _ in range(2, num_terms):
next_term = a + b
fibonacci_sequence.append(next_term)
a, b = b, next_term
return fibonacci_sequence
num_terms = int(input("Enter the number of terms in the Fibonacci sequence: "))
fibonacci_sequence = generate_fibonacci_sequence(num_terms)
print("Fibonacci sequence:")
print(fibonacci_sequence)
6)import math
def calculate_circle_area(radius):
return math.pi * (radius ** 2)
def calculate_circle_perimeter(radius):
return 2 * math.pi * radius
def calculate_rectangle_area(length, width):
return length * width
def calculate_rectangle_perimeter(length, width):
return 2 * (length + width)
def calculate_triangle_area(base, height):
return 0.5 * base * height
def calculate_triangle_perimeter(side1, side2, side3):
return side1 + side2 + side3
print("Geometric Shape Calculator")
shape = input("Enter the geometric shape (circle, rectangle, triangle): ").lower()
if shape == "circle":
radius = float(input("Enter the radius of the circle: "))
area = calculate_circle_area(radius)
perimeter = calculate_circle_perimeter(radius)
print(f"Area of the circle: {area}")
print(f"Perimeter of the circle: {perimeter}")
elif shape == "rectangle":
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = calculate_rectangle_area(length, width)
perimeter = calculate_rectangle_perimeter(length, width)
print(f"Area of the rectangle: {area}")
print(f"Perimeter of the rectangle: {perimeter}")
elif shape == "triangle":
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = calculate_triangle_area(base, height)
perimeter = calculate_triangle_perimeter(base, base, base)
print(f"Area of the triangle: {area}")
print(f"Perimeter of the triangle: {perimeter}")
else:
print("Invalid shape. Please enter 'circle', 'rectangle', or 'triangle'.")
7)def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius:.2f} degrees Celsius is equal to {fahrenheit:.2f} degrees Fahrenheit.")
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit:.2f} degrees Fahrenheit is equal to {celsius:.2f} degrees Celsius.")
8)def count_upper_and_lower_case_letters(input_str):
upper_count = 0
lower_count = 0
for char in input_str:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
return upper_count, lower_count
input_str = input("Enter a string: ")
upper_count, lower_count = count_upper_and_lower_case_letters(input_str)
print(f"Number of uppercase letters: {upper_count}")
print(f"Number of lowercase letters: {lower_count}")
9)def perform_arithmetic_operations(complex_num1, complex_num2):
addition_result = complex_num1 + complex_num2
subtraction_result = complex_num1 - complex_num2
multiplication_result = complex_num1 * complex_num2
division_result = complex_num1 / complex_num2
return addition_result, subtraction_result, multiplication_result, division_result
complex_num1 = complex(2, 3) # 2 + 3j
complex_num2 = complex(1, 4) # 1 + 4j
addition, subtraction, multiplication, division = perform_arithmetic_operations(complex_num1,
complex_num2)
print("Complex Numbers:")
print("Complex Number 1:", complex_num1)
print("Complex Number 2:", complex_num2)
print("\nArithmetic Operations:")
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
Download