Close
Close

Practice questions on Have your own function

Level 1

1.
Print multiplication table of 12 using recursion.
def table(n,i):
  print n*i
  i=i+1
  if i<=10:
    table(n,i)
table(12,1)

2.
Write a function to calculate area and perimeter of a rectangle.

3.
Write a function to calculate area and circumference of a circle.

4.
Write a function to calculate power of a number raised to other. E.g.- ab.

5.
Write a function to tell user if he/she is able to vote or not.
( Consider minimum age of voting to be 18. )

6.
Write a function to calculate power of a number raised to other ( ab ) using recursion.
def power(a,b):
  if b == 1:
    return a
  else:
    return a*power(a,b-1)
print power(6,2)

7.
Write a function “perfect()” that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000.
[An integer number is said to be “perfect number” if its factors, including 1(but not the number itself), sum to the number. E.g., 6 is a perfect number because 6=1+2+3].
def perfect(n):
  sum = 0
  for i in range(1,n):
    if n%i == 0:
      sum = sum + i
  if sum == n:
    return True
  else:
    return False
for i in range(1,1001):
  if perfect(i):
    print i

8.
Write a function to check if a number is even or not.

9.
Write a function to check if a number is prime or not.

10.
Write a function to find factorial of a number but also store the factorials calculated in a dictionary as done in the Fibonacci series example.

Level 2

Level 3

Ask Yours
Post Yours