Page 272 - Computer Science Class 11 With Functions
P. 272
total = total + term
return total
num = int(input('Enter last number in the series'))
result = sumSeries(num)
print('Sum of ', num, 'terms is:' , result)
2. Sambhav works in marketing department of an IT firm named Diamond Corporation. For the starting slide of his presentation,
his manager wants to display the following pattern on the screen. The manager wants the size of the pattern to be generic,
so that it can be decided later.
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
Sambhav has been assigned a task to write a program in Python to display the given pattern. Help him complete the
task.
Ans. def diamond(n):
for i in range(n):
for j in range(n-i-1):
print(' ', end='')
for j in range(2*i + 1):
print('*', end='')
print()
for i in range(n-1):
for j in range(i+1):
print(' ', end='')
for j in range(2*(n-i-1)-1):
print('*', end='')
print()
num = int(input('Enter the number'))
diamond(num)
3. You must have studied HCF(Highest Common Factor) and LCM ( Least Common Multiple) in mathematics. Your mathematics
teacher wants you to write a program in Python to accept two numbers and then calculates and display LCM and HCF for
those two numbers.
Ans. def computeHCF_LCM(num1, num2):
'''
Objective: To compute LCM and HCF
Input Parameters: num1, num2 - numeric value
Return Value: LCM, HCF - numeric value
'''
if num1>num2:
small = num2
else:
small = num1
for x in range(1, small+1):
if (num1%x == 0) and (num2%x == 0):
270 Touchpad Computer Science-XI

