Page 427 - Computer Science Class 11 With Functions
P. 427
Program 8
Write a function checkPrime(num) that takes num as an input and checks whether num is a prime or not. Prime
numbers are natural numbers that are divisible by only 1 and the number itself. Invoke the function to print an
appropriate message for the user-entered number.
Ans. def checkPrime(num):
'''
Objective: To check whether a number is prime.
Input: num: the number to be tested for primeness
Return Value: The message indicating whether n is prime
'''
upperLimit = num
for i in range(2, num):
if num % i == 0:
print('Not a prime number, because',num, '=', i, '*', num // i)
#i divides num
break
else:
print(num, 'is a prime number')
num = int(input("Enter a number to check for prime: "))
checkPrime(num)
Program 9
Write a function fibonacci(num) that takes num as an input and prints the fibonacci series until num terms.
Invoke the function to print the fibonacci series for the user-entered nth term.
Ans. def fibonacci(num):
'''
Objective : To calculate the fibonacci series
Input Parameter : num - numeric value
Return Value : None
'''
x = 0
y = 1
if num == 1:
print(x)
else:
print(x,y, end=' ')
for i in range(2,num):
z = x + y
Practical 425

