Page 269 - Computer Science Class 11 With Functions
P. 269
Ans. # To generate the sequence : -5 10 -15 20 -25.......n
limit = int(input('Enter a +ve integer as upper limit: '))
sign = -1
print('Sequence:')
for num in range(5, limit, 5):
value = num * sign
sign = sign * -1
print(value, end = ' ')
11. Write a program to accept a number and display the following pattern.
Ans. num = int(input('Enter a number : '))
spaces = num * 2 - 1 #calculate the spaces
for i in range(1, num + 1):
print(' ' * spaces, end='') #Leading Spaces
for j in range(i, 0, -1): #Count down
print(j, end=' ')
for j in range(2, i + 1): #Count up
print(j, end=' ')
spaces = spaces - 2
print()
12. Write a program to accept two numbers and display their LCM (Least Common Multiple).
Ans. # To find LCM
num1 = int(input('Enter the first number: '))
num2 = int(input('Enter the second number: '))
for i in range(1, num2):
if num1 * i % num2 == 0:
lcm = num1 * i
break
else:
lcm=num1*num2
print('The LCM is:', lcm)
13. When the following code is executed, what output will be produced. Also, indicate which of the statements labelled s1,
s2, s3, s4, and s5 will not get executed?
01 counter = 1
02 while counter <= 16:
03 if counter < 16:
04 print('counter: ', counter) #s1
05 else:
06 break #s2
07 print('Stop') #s3
08 counter += 4 #s4
09 else:
10 print('Exit from loop') #s5
Looping in Python 267

