Week 10
Week 10
PREJAN RAJA S
RA2211047010019
B.TECH- AI- A
def calculate_result(nums):
total_sum = sum(nums)
if 120 <= total_sum <= 320:
return 200
else:
return total_sum
result = calculate_result(nums)
print("Result:", result)
Output:
Enter number 1: 80
Enter number 2: 90
Result: 200
1
WEEK – 10
RA2211003011297
Output:
2
WEEK – 10
RA2211003011297
result = calculate_factorial(num)
print(f"The factorial of {num} is {result}")
Output:
Enter a number: 5
3
WEEK – 10
RA2211003011297
def is_prime(number):
if number <= 1:
return False
if number == 2:
return True
if number % 2 == 0:
return False
for i in range(3, int(number**0.5) + 1, 2):
if number % i == 0:
return False
return True
if is_even(num):
print(f"{num} is even.")
else:
print(f"{num} is odd.")
if is_prime(num):
print(f"{num} is a prime number.")
else:
4
WEEK – 10
RA2211003011297
def is_palindrome(input_string):
# Remove spaces and convert to lowercase for case-insensitive
palindrome check
cleaned_string = input_string.replace(" ", "").lower()
reversed_str = reverse_string(cleaned_string)
return cleaned_string == reversed_str
reversed_str = reverse_string(input_str)
print("Reversed string:", reversed_str)
if is_palindrome(input_str):
print("The input string is a palindrome.")
else:
print("The input string is not a palindrome.")
5
WEEK – 10
RA2211003011297
Output:
Enter a string: radar
Reversed string: radar
The input string is a palindrome.
for _ in range(n):
sequence.append(a)
a, b = b, a + b
return sequence
if n <= 0:
print("Please enter a positive integer.")
else:
fibonacci_sequence = generate_fibonacci_sequence(n)
print("Fibonacci Sequence:")
for num in fibonacci_sequence:
print(num, end=" ")
6
WEEK – 10
RA2211003011297
Output:
Enter the number of terms for the Fibonacci sequence: 6
Fibonacci Sequence:
0 11235
def calculate_circle_area(radius):
return math.pi * (radius ** 2)
def calculate_circle_perimeter(radius):
return 2 * math.pi * radius
7
WEEK – 10
RA2211003011297
print("1. Circle")
print("2. Rectangle")
print("3. Triangle")
if choice == 1:
radius = float(input("Enter the radius of the circle: "))
area = calculate_circle_area(radius)
perimeter = calculate_circle_perimeter(radius)
print(f"Circle Area: {area}")
print(f"Circle Perimeter: {perimeter}")
elif choice == 2:
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"Rectangle Area: {area}")
print(f"Rectangle Perimeter: {perimeter}")
elif choice == 3:
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = calculate_triangle_area(base, height)
side1 = float(input("Enter the length of side 1: "))
side2 = float(input("Enter the length of side 2: "))
side3 = float(input("Enter the length of side 3: "))
perimeter = calculate_triangle_perimeter(side1, side2, side3)
print(f"Triangle Area: {area}")
print(f"Triangle Perimeter: {perimeter}")
else:
print("Invalid choice. Please choose 1, 2, or 3.")
Output:
Choose a geometric shape:
1. Circle
2. Rectangle
3. Triangle
Enter your choice (1/2/3): 1
Enter the radius of the circle:
7
Circle Area: 153.93804002589985
Circle Perimeter: 43.982297150257104
8
WEEK – 10
RA2211003011297
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius
if unit == "C":
fahrenheit = celsius_to_fahrenheit(temperature)
print(f"{temperature}°C is equal to {fahrenheit}°F.")
elif unit == "F":
celsius = fahrenheit_to_celsius(temperature)
print(f"{temperature}°F is equal to {celsius}°C.")
else:
print("Invalid unit. Please enter 'C' for Celsius or 'F' for
Fahrenheit.")
Output:
Enter the temperature: 100
Enter the unit (C for Celsius, F for Fahrenheit): F
100.0°F is equal to 37.77777777777778°C.
9
WEEK – 10
RA2211003011297
Output:
Enter a string: PYTHON Programming
10
WEEK – 10
RA2211003011297
11
WEEK – 10
RA2211003011297
Output:
Enter the real part of the first complex number: 3
HACKERRANK:
12
WEEK – 10
RA2211003011297
13
WEEK – 10
RA2211003011297
14
WEEK – 10
RA2211003011297
15