NAME – AAMIR
CLASS – XII A
ROLL NO – 1
Practical file
PROGRAM 1: find the factorial of natural number
def factorial(n):
if n < 0:
return None # Factorial is not defined for negative numbers
elif n == 0 or n == 1:
return 1
else:
result = 1
for i in range(2, n + 1):
result *= i
return result
# Example usage:
n = int(input("Enter a natural number: "))
result = factorial(n)
if result is not None:
print(f"The factorial of {n} is {result}")
else:
print("Factorial is not defined for negative numbers.")
OUTPUT :
PROGRAM 2 : find the sum all elements of a list
numbers = [10, 20, 30, 40, 50]
total_sum = sum(numbers)
print("Sum of all elements:", total_sum)
OUTPUT :
PROGRAM 3 : find code to compute the nth Fibonacci
number
def fibonacci_iterative(n):
if n <= 0:
return None
elif n == 1:
return 0 # First Fibonacci number is 0
elif n == 2:
return 1
a, b = 0, 1
for _ in range(3, n + 1):
c=a+b
a=b
b=c
return b
n = int(input("Enter the value of n to compute nth Fibonacci number: "))
result = fibonacci_iterative(n)
if result is not None:
print(f"The {n}th Fibonacci number is {result}")
else:
print("Fibonacci sequence is not defined for n <= 0")
OUTPUT :
PROGRAM 4 : calculate the simple interest by using
function
def calculate_simple_interest(principal, rate, time):
"""
Calculate the simple interest based on principal, rate, and time.
Args:
principal (float): The principal amount.
rate (float): The rate of interest per annum.
time (float): The time period in years.
Returns:
float: The calculated simple interest.
"""
simple_interest = (principal * rate * time) / 100
return simple_interest
# Example usage:
P = float(input("Enter the principal amount: "))
R = float(input("Enter the rate of interest per annum: "))
T = float(input("Enter the time period in years: "))
SI = calculate_simple_interest(P, R, T)
print(f"The Simple Interest for principal {P}, rate {R}%, and time {T} years is:
{SI}")
OUTPUT :
PROGRAM 5 : program to calculate the arithmetic
operators by using function
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Division by zero is not allowed"
def arithmetic_operations(a, b):
print("Addition:", add(a, b))
print("Subtraction:", subtract(a, b))
print("Multiplication:", multiply(a, b))
print("Division:", divide(a, b))
# Example usage:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
arithmetic_operations(num1, num2)
OUTPUT :
PROGRAM 6 : program to enter to numbers and swap
their values by using function
def swap(a, b):
# Swapping values
a, b = b, a
return a, b
# Example usage:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print("Before swapping:")
print("num1 =", num1)
print("num2 =", num2)
num1, num2 = swap(num1, num2)
print("After swapping:")
print("num1 =", num1)
print("num2 =", num2)
OUTPUT :