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

Class XII - Computer Science Assessed Homework 1

Uploaded by

Shashank
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)
9 views

Class XII - Computer Science Assessed Homework 1

Uploaded by

Shashank
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/ 4

Computer Science – Assessed Homework 1

1) Write a program to calculate the factorial of a given number:


def fact(num):
if num==0:
return 1
else:
return(num*fact(num-1))
num=int(input('Enter the number: '))
print('The factorial of',num,'is ',fact(num))

Output:
Enter the number: 6
The factorial of 6 is 720

2) Write a program to generate the Fibonacci series.


def fib(num):
if num<=1:
return num
else:
return(fib(num-1) + fib(num-2))
num = int(input("Enter number of terms: "))
print("Fibonacci sequence:")
for i in range(num):
print(fib(i))

Output:
Enter the number of terms: 7
Fibonacci Sequence:
0
1
1
2
3
5
8

3) Write a program to calculate the LCM.


def HCF(n1, n2):
if n2 == 0:
return n1
else:
return HCF(n2, n1 % n2)
def LCM(n1, n2):
return (n1 * n2) // HCF(n1, n2)
n1=int(input('Enter first number: '))
n2=int(input('Enter second number: '))
print("LCM of",n1, "and",n2, "is", LCM(n1, n2))

Output:
Enter first number: 36
Enter second number: 24
LCM of 36 and 24 is 72.

4) Write a program to evaluate a to the power b.


def pow(n1,n2):
if n2!=0:
return n1*pow(n1,n2-1)
else:
return 1
n1=int(input('Enter the number: '))
n2=int(input('Enter the power: '))
print(n1,'power',n2,'is: ',pow(n1,n2))

Output:
Enter the number: 3
Enter the power: 4
3 power 4 is: 81

5) Write a program to search an element using binary search

def Binary_Search(arr,n,lb,ub,X):
if(lb>ub):
print("Not found!")
else:
mid = int((lb+ub)/2)
if(arr[mid]==X):
print('Element found at position: ',mid+1)
elif(arr[mid]>X):
Binary_Search(arr,n,lb,mid,X);
elif(arr[mid]<X):
Binary_Search(arr,n,mid+1,ub,X);
arr = []
a=int(input('Enter the number of elements: '))
for i in range(a):
arr.append(int(input('Enter the element: ')))
n = len(arr)
X = int(input('Enter the element you want to search: '))
Binary_Search(arr,n,0,n-1,X)
Output:
Enter the number of elements: 7
Enter the element: 2
Enter the element: 65
Enter the element: 789
Enter the element: 324
Enter the element: 5
Enter the element: 1
Enter the element: 51
Enter the element you want to search: 324
Element found at position: 4

You might also like