Page 173 - Information_Practice_Fliipbook_Class11
P. 173
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):
HCF=x
LCM=(num1*num2)/HCF
return LCM, HCF
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
LCM, HCF = computeHCF_LCM(num1, num2)
print('The LCM of ', num1, ' and ', num2, ' is ', LCM)
print('The HCF of ', num1, ' and ', num2, ' is ', HCF)
Assessment
A. Multiple Choice questions
1. Which of the following will return a sequence of numbers within a specified range?
a. range() b. Range() c. Sequence() d. sequence()
2. Which of the following options is the correct sequence that will be returned by the function range(-4, 8, 4)?
a. [-4, 0, 4] b. [4, 0, -4] c. [-4, 0, 4, 8] d. [8, 4, 0, -4]
3. Which of the following options represents the correct sequence that will be returned by the function range(20, 15)?
a. [20, 19, 18, 17, 16, 15]
b. [20, 19, 18, 17, 16]
c. []
d. [15, 16, 17, 18, 19]
Looping in Python 159

