Professional Documents
Culture Documents
Vinay KR..! Practical File
Vinay KR..! Practical File
OPG WORLD
SCHOOL
CLASS XII
INDEX
S. No. Program Page Teachers
Name Number remarks
1. CALCULAT 4,5
OR
2. HCF OF 6,7
TWO
NUMBERS
3. FACTORIAL 8,9
USING
RECURSION
4. SUM OF N 10 , 11
NUMBERS
USING
RECURSION
5. FIBONACCI 12 , 13
SERIES
USING
RECURSION
Page |3
CERTIFICATE
Date:
Registration No:
Signature Signature
(Internal Examiner) (External Examiner)
Page |4
PROGRAM – 1
Aim : Write a program to do calculator operations using functions.
Program :
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
Page |5
Output :
Page |6
Program – 2
Aim : Write a program to find the HCF of two numbers using
functions.
Program :
def compute_hcf(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
num1 = 54
num2 = 24
Output :
Page |7
Page |8
Program – 3
Aim : Write a recursive code to find the Factorial.
Program :
def recur_factorial(n):
if n == 1 or n == 0:
return n
else:
return n*recur_factorial(n-1)
num = 9
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
Output :
Page |9
PROGRAM – 4
Aim : Write a recursive code to find the sum of N numbers.
Program :
def recur_sum(n):
if n <= 1:
P a g e | 10
return n
else:
return n + recur_sum(n-1)
num = 16
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))
Output :
P a g e | 11
PROGRAM – 5
Aim : Write a recursive code to find the Fibonacci series.
Program :
def f(n):
if n>=2:
return f(n-1)+f(n-2)
elif n==1:
return 1
elif n==0:
P a g e | 12
return 0
n=int(input("Enter a number = "))
for i in range(0,n):
print(f(i))
Output :