0% found this document useful (0 votes)
38 views15 pages

Week 10

The document discusses 10 Python programs that implement various functions and operations. The programs include calculating sums and maximums, finding factorials, checking for palindromes, generating Fibonacci sequences, calculating area and perimeter of shapes, converting between temperature scales, counting letters in strings, and performing operations on complex numbers.

Uploaded by

srikrishna30233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views15 pages

Week 10

The document discusses 10 Python programs that implement various functions and operations. The programs include calculating sums and maximums, finding factorials, checking for palindromes, generating Fibonacci sequences, calculating area and perimeter of shapes, converting between temperature scales, counting letters in strings, and performing operations on complex numbers.

Uploaded by

srikrishna30233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SIMPLE PYTHON PROGRAM TO IMPLEMENT FUNCTIONS

PREJAN RAJA S
RA2211047010019
B.TECH- AI- A

1. Write a python program to calculate the sum of Two


numbers and Three numbers.However, if the sum is
between 120 to 320 it will return 200.
Source Code:
def calculate_sum_two_numbers(num1, num2):
return num1 + num2

def calculate_sum_three_numbers(num1, num2, num3):


return num1 + num2 + num3

def calculate_result(nums):
total_sum = sum(nums)
if 120 <= total_sum <= 320:
return 200
else:
return total_sum

# Test the program


num_count = int(input("Enter the number of numbers (2 or 3): "))
if num_count not in [2, 3]:
print("Invalid input. Please enter 2 or 3.")
else:
nums = []
for i in range(num_count):
num = float(input(f"Enter number {i+1}: "))
nums.append(num)

result = calculate_result(nums)
print("Result:", result)

Output:

Enter the number of numbers (2 or 3): 2

Enter number 1: 80

Enter number 2: 90

Result: 200

1
WEEK – 10
RA2211003011297

2. Implement a python function to find the Maximum


of Three numbers.
Source Code:
def find_maximum_of_three_numbers(num1, num2, num3):
if num1 >= num2 and num1 >= num3:
return num1
elif num2 >= num1 and num2 >= num3:
return num2
else:
return num3

# Get user input for three numbers


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

maximum = find_maximum_of_three_numbers(num1, num2, num3)


print(f"The maximum of {num1}, {num2}, and {num3} is {maximum}")

Output:

Enter the first number: 5

Enter the second number: 15

Enter the third number: 25

The maximum of 5.0, 15.0, and 25.0 is 25.0

2
WEEK – 10
RA2211003011297

3. Write a python program to calculate the Factorial of


a given number.
Source Code:
def calculate_factorial(number):
if number < 0:
return "Factorial is not defined for negative numbers"
elif number == 0:
return 1
else:
factorial = 1
for i in range(1, number + 1):
factorial *= i
return factorial

# Get user input for the number


num = int(input("Enter a number: "))

result = calculate_factorial(num)
print(f"The factorial of {num} is {result}")

Output:

Enter a number: 5

The factorial of 5 is 120

3
WEEK – 10
RA2211003011297

4. Write a python program to Check if a Number is


Even or Odd and also check whether it is Prime or
not.
Source Code:
def is_even(number):
return number % 2 == 0

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

# Get user input for the number


num = int(input("Enter a number: "))

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

print(f"{num} is not a prime number.")


Output:
Enter a number: 7
7 is odd.
7 is a prime number.

5. Implement a python function to Reverse a given


String and also check for palindrome or not.
Source Code:
def reverse_string(input_string):
# Reverse the string using slicing
reversed_str = input_string[::-1]
return reversed_str

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

# Test the functions


input_str = input("Enter a string: ")

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.

6. Write a python program to Generate Fibonacci


Sequence.
Source Code:
def generate_fibonacci_sequence(n):
sequence = []
a, b = 0, 1

for _ in range(n):
sequence.append(a)
a, b = b, a + b

return sequence

# Get the number of terms for the Fibonacci sequence


n = int(input("Enter the number of terms for the Fibonacci sequen
ce: "))

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

7. Write a python program to calculate the area and


perimeter of different geometric shapes (circle,
rectangle, triangle, etc.).
Source Code:
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

# Menu to choose a geometric shape


print("Choose a geometric shape:")

7
WEEK – 10
RA2211003011297

print("1. Circle")
print("2. Rectangle")
print("3. Triangle")

choice = int(input("Enter your choice (1/2/3): "))

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

8. Implement a python function to Convert Celsius to


Fahrenheit and Fahrenheit to Celsius.
Source Code:
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit

def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius

# Get user input for the temperature and unit


temperature = float(input("Enter the temperature: "))
unit = input("Enter the unit (C for Celsius, F for Fahrenheit): "
).upper()

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

9. Write a Python program that accepts a string and


counts the number of upper and lower case letters.
Source Code:
def count_upper_and_lower_case_letters(input_string):
uppercase_count = 0
lowercase_count = 0

for char in input_string:


if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1

return uppercase_count, lowercase_count

# Input a string from the user


input_string = input("Enter a string: ")

upper_count, lower_count = count_upper_and_lower_case_letters(inp


ut_string)

print("Number of uppercase letters:", upper_count)


print("Number of lowercase letters:", lower_count)

Output:
Enter a string: PYTHON Programming

Number of uppercase letters: 7

Number of lowercase letters: 10

10
WEEK – 10
RA2211003011297

10. Write a python program to perform Arithmetic


operations on Complex Numbers.
Source Code:
# Input complex numbers from the user
real1 = float(input("Enter the real part of the first complex num
ber: "))
imag1 = float(input("Enter the imaginary part of the first comple
x number: "))
complex_num1 = complex(real1, imag1)

real2 = float(input("Enter the real part of the second complex nu


mber: "))
imag2 = float(input("Enter the imaginary part of the second compl
ex number: "))
complex_num2 = complex(real2, imag2)

# Perform arithmetic operations


addition_result = complex_num1 + complex_num2
subtraction_result = complex_num1 - complex_num2
multiplication_result = complex_num1 * complex_num2
division_result = complex_num1 / complex_num2

# Display the results


print("\nComplex Number 1:", complex_num1)
print("Complex Number 2:", complex_num2)
print("Addition Result:", addition_result)
print("Subtraction Result:", subtraction_result)
print("Multiplication Result:", multiplication_result)
print("Division Result:", division_result)

11
WEEK – 10
RA2211003011297

Output:
Enter the real part of the first complex number: 3

Enter the imaginary part of the first complex number: 2

Enter the real part of the second complex number: 1

Enter the imaginary part of the second complex number: 4

Complex Number 1: (3+2j)

Complex Number 2: (1+4j)

Addition Result: (4+6j)

Subtraction Result: (2-2j)

Multiplication Result: (-5+14j)

Division Result: (0.6470588235294118-0.5882352941176471j)

HACKERRANK:

12
WEEK – 10
RA2211003011297

13
WEEK – 10
RA2211003011297

14
WEEK – 10
RA2211003011297

15

You might also like