Page 257 - Computer Science V2.0 Class 11
P. 257
maximum = n3
return maximum
2. Write a function max2(n1,n2)that finds the maximum of two numbers and then make use of the function max2(n1,n2)
to write the function max3(n1, n2, n3) to find the maximum of the numbers n1, n2, and n3.
Ans. def max2(n1, n2):
'''
Objective: To find maximum of two numbers
Input Parameters: n1, n2 - numeric values
Return Value: maximum of n1, n2 - numeric value
'''
'''
Approach:
Compare two numbers and return the maximum of two numbers.
'''
if n1 > n2:
return n1
else:
return n2
def max3(n1, n2, n3):
'''
Objective: To find maximum of three numbers
Input Parameters: n1, n2, n3 - numeric values
Return Value: maximum of n1, n2, n3 - numeric value
Approach: Find maximum of two numbers at a time
'''
return max2(max2(n1, n2), n3)
3. Write a program to find the number of four-digit palindrome numbers divisible by 11. You are not allowed to use a function
palindrome(string)that returns True if the input string is a palindrome and False otherwise.
Ans. def div11Palindromes():
'''
Objective: To find the number of four-digit palindrome divisible by 11
Input Parameter: None
Return Value: count of palindromes divisible by 11
'''
count = 0
start = 1001 #1001 is divisible by 11
finish = 10000
for n in range(start, finish, 11):
#if unit digit equals thousand's digit and
# tens digit equals hundreds digit,
#then n is a palindrome
if ((n//1000)%10 == n%10) and ((n//100)%10 == (n//10)%10):
count+=1
return count
#Print the number of 4-digit palindromes divisible by 11
print('No of 4-digit palindromes divisible by 11 is ', div11Palindromes())
Modules 243

