0% found this document useful (0 votes)
3 views

cs project file

The document outlines several Python programs for basic arithmetic operations, generating Fibonacci series, calculating factorials and sums of lists, and implementing mathematical functions like square and logarithm. Each section includes an aim and source code for the respective program, demonstrating user interaction and function usage. The programs are designed to be menu-driven and handle user input effectively.

Uploaded by

devilh12334
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

cs project file

The document outlines several Python programs for basic arithmetic operations, generating Fibonacci series, calculating factorials and sums of lists, and implementing mathematical functions like square and logarithm. Each section includes an aim and source code for the respective program, demonstrating user interaction and function usage. The programs are designed to be menu-driven and handle user input effectively.

Uploaded by

devilh12334
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Menu-Driven Arithmetic Operations

AIM: To write a menu-driven Python Program to perform Arithmetic operations (+, -, *, /) based on user choice.

SOURCE CODE:
def arithmetic_operations():
while True:
print("Select operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")

choice = input("Enter choice: ")

if choice in ['1', '2', '3', '4']:


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

if choice == '1':
print("Result:", num1 + num2)
elif choice == '2':
print("Result:", num1 - num2)
elif choice == '3':
print("Result:", num1 * num2)
elif choice == '4':
print("Result:", num1 / num2 if num2 != 0 else "Division by zero not
allowed")
elif choice == '5':
break
else:
print("Invalid choice, try again.")

arithmetic_operations()
2. Fibonacci Series

AIM: To write a Python Program to display Fibonacci Series up to 'n' numbers.

SOURCE CODE:
def fibonacci(n):
sequence = [0, 1]
for _ in range(n - 2):
sequence.append(sequence[-1] + sequence[-2])
return sequence[:n]

n = int(input("Enter number of terms: "))


print("Fibonacci Series:", fibonacci(n))
3. Factorial and Sum of List

AIM: To write a menu-driven Python Program to find Factorial and sum of list of numbers using function.

SOURCE CODE:
import math

def menu():
print("1. Calculate Factorial")
print("2. Sum of List")
print("3. Exit")

def factorial(n):
return math.factorial(n)

def sum_list(lst):
return sum(lst)

while True:
menu()
choice = input("Enter choice: ")

if choice == '1':
num = int(input("Enter number: "))
print("Factorial:", factorial(num))
elif choice == '2':
numbers = list(map(int, input("Enter numbers separated by space: ").split()))
print("Sum:", sum_list(numbers))
elif choice == '3':
break
else:
print("Invalid choice, try again.")
4. Implementing Mathematical Functions

AIM: To write a Python program to implement mathematical functions such as square, logarithm, and cube.

SOURCE CODE:
import math

def square(n):
return n ** 2

def logarithm(n):
return math.log10(n)

def cube(n):
return n ** 3

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


print("Square:", square(num))
print("Logarithm (base 10):", logarithm(num))
print("Cube:", cube(num))

You might also like