(Grade X)Chapter 4- Programming in Python-Function

 

 

 

Part 2- User Defined Function 

 

1. Write a program to calculate the area of a circle using function.

def area (r):

    a=(22/7)*r*r

    return a

 

r=float (input(" Enter the radius:"))

cir=area(r)

print(f"Area of the circle is {cir}")

 

2. Write a program to calculate the volume of a cylinder.

def volume (r,h):

    v=22/7*r*r*h

    return v

 

r = float (input(" Enter the radius:"))

h = float (input(" Enter the height: "))

vol=volume (r,h)

print("Volume of the cylinder is", vol)

 

Assignment: Pg 195 Lab sheet 1- 1,2,3

 

3. Write a program to display the greatest number among two different numbers using function.

def great(a, b):

    if a > b:

        gre = a

    else:

        gre = b

    return gre

 

a = int(input("Enter the first number: "))

b =int (input("Enter the second number: "))

g=great(a, b)

print(f"The greatest number is {g}")

 

4. Write a program to display the smallest number among any three different numbers given by the user using function.

def smallest(a,b,c):

    if a < b and a < c:

        return a

    elif b<a and b <c:

        return b

    else:

        return c

 

a=float(input("Enter the first number"))

b=float(input("Enter the second number"))

c=float(input("Enter the third number"))

s=smallest(a,b,c)

print("The smallest number is",s)

 

5. Write a program to display whether the given number is divisible by 7 or not using function.

def divisible(n):

    if n % 7 ==0:

        print("Number is divisible by 7")

    else:

        print("Number is not divisible by 7")

 

n=int(input("Enter any number: "))

divisible(n)

 

6. Write a program to input any number and check whether the input number is positive, negative or neutral using function.

def check(n):

    if n > 0:

        return "The given number is positive"

    elif n < 0:

        return "The given number is negative"

    else:

        return "The given number is neutral"

 

n=int(input("Enter the number: "))

z=check(n)

print(z)

 

Assignment: Pg 195 Lab sheet 1-4,5,6,7,8

 

7. Write a program to create a user defined function which display the sum of all the numbers from 1 to 50,

def sum_numbers():

    s = 0

    for i in range(1, 51):

         s = s + i

    return s

   

p= sum_numbers()

print("Sum of all the numbers from 1 to 50 is", p)

 

8. Write a program to display the factors of the given number using function.

def factors(n):

    print("Factors are:")

    for i in range(1, n + 1):

        if n% i ==0:

            print(i)

           

num = int(input("Enter the number: "))

factors(num)

 

9. Write a program to display whether the given number is prime or composite using function.

 def primecomposite(n):

    c = 0

    for i in range(1, n + 1):

        if n%i==0:

            c = c + 1

    if c == 2:

        print("The number is prime")

    else:

      print("The number is composite")

 

n=int(input("Enter the number: "))

primecomposite(n)

Dry run table if n = 6

n

i

n % i == 0

c

Output

 6

 

 

0

 

1

Yes

1

The number is composite

2

Yes

2

 

3

Yes

3

 

4

No


 

5

No


 

6

Yes

4

 

 

10. Write a program to display the given number in reverse order by creating defined function.

 def reverse(n):

    s = 0

    while n != 0:

        r = n % 10

        s = s * 10 + r

        n = n // 10

    return s

 

n = int(input("Enter a number: "))

rev = reverse(n)

print("Reversed number is:", rev)

Dry run table if n = 123

n

r

s

Output

123

3

0

Reversed number is:321

12

2

3

1

1

32

 

0

321

 

 11. Write a program to display whether the given number is palindrome or not by using function.

def palin(n):

    temp = n

    s = 0

    while n != 0:

        r = n % 10

        s = s * 10 + r

        n = n // 10

    if temp == s:

        print("The number is a palindrome")

    else:

        print("The number isn't a palindrome")

 

n = int(input("Enter a number: "))

palin(n)

Dry run table, if n=121

n

temp

r

s

Output

121

121

1

0

The number is a palindrome

12

 

2

1

 

1

 

1

12

 

 

 

 

121

 

 

Assignment: Pg 195 Lab sheet 1-9,10,11,12,13,14

 

12. Write a program to store ten numbers in the list. The program should then count the even numbers by creating function.

def count_even(num):

    c = 0

    for n in num:

        if n % 2 == 0:

            c = c + 1

    return c

 

num = []

for i in range(10):

    n = int(input("Enter number: "))

    num.append(n)

count = count_even(num)

print("Total even numbers:", count)

 

13. Write a program to input ten numbers from the user and store them in the list the program should then display the greatest number among ten numbers.

def greatest(num):

    gre = max(num)

    return gre

 

num = []

print("Enter 10 numbers:")

for x in range(10):

    n = int(input("Enter number: "))

    num.append(n)

g = greatest(num)

print(f"Greatest number in the list is {g}")

 

14. Write a program to store five numbers in the list. The program should display the average of the numbers by creating user defined function.

def average(n):

    s = sum(n)

    avg = s / 5

    return avg

 

n = []

for i in range(5):

    x = float(input("Enter number: "))

    n.append(x)

a = average(n)

print("Average =", a)

 

15. Write a program to display the longest word among two different words function.

def longest(str1, str2):

    a = len(str1)

    b = len(str2)

    if a > b:

        print(f"The longest word is {str1}")

    else:

        print(f"The longest word is {str2}")

 

str1 = input("Enter the first word: ")

str2 = input("Enter the second word: ")

longest(str1, str2)

 

16. Write a program to count the number of vowels present in the given word using function.

def vowels(w):

    c = 0

    u = w.upper()

    for i in u:

        if i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U':

            c = c + 1

    return c

 

w = input("Enter the word: ")

v = vowels(w)

print("Number of vowels are", v)

 

17. Write a program to display the given word in reverse order using function.

def reverse(w):

    rev = ""

    for i in w:

        rev = i + rev

    return rev

 

w = input("Enter the word: ")

r = reverse(w)

print(f"Reverse of the word is {r}")

 

18. Write a program to display the area and volume of a room using user defined function.

def area(l, b):

    a = l * b

    return a

 

def volume(l, b, h):

    v = l * b * h

    return v

 

l = float(input("Enter the length: "))

b = float(input("Enter the breadth: "))

h = float(input("Enter the height: "))

ar = area(l, b)

vol = volume(l, b, h)

print("Area of room is", ar)

print("Volume of a room is", vol)

 

19. Write a program to calculate the area and volume of a sphere by creating user defined function.

def area(r):

    a = 4 * 22 / 7 * r * r

    return a

 

def volume(r):

    v = 4 / 3 * 22 / 7 * r * r * r

    return v

 

r = float(input("Enter the radius: "))

ar = area(r)

print(f"Area of the sphere is {ar}")

vol = volume(r)

print(f"Volume of the sphere is {vol}")

20. Write a program to input two different numbers from the user. Create defined average() to display the average of two numbers and greatest() to display the greatest number among two numbers.

def average(a, b):

    avg = (a + b) / 2

    return avg

 

def greatest(a, b):

    if a > b:

        print("The greatest number is", a)

    else:

        print("The greatest number is", b)

 

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

greatest(a, b)

av = average(a, b)

print("Average is", av)

Assignment: Pg 95 Lab sheet 1

 21. Write the output of the following program. Show with dry run in a table.

 a.

def result(x, y):

     if x % y == 0:

        x += y

    else:

        x *= y

    return x

 x = 7

y = 2

s = result(x, y)

print("The value of x is", s)

x

y

Condition x % y == 0

s

Output

7

2

7 % 2 = 1 → False

14

The value of x is 14

b.

def series(n):

    for i in range(1, 6):

        print(n, end=" ")

        if n % 2 == 0:

            n = n // 2

        else:

           n = 3 * n + 1

n = 9

series(n)

i

n

n%2==0

Output

1

9

Odd (false)

9 28 14 7 22

2

28

Even (true)

 

3

14

Even (true)

 

4

7

Odd (false)

 

5

22

Even (true)

 

 

11

 

 

 c.

def number(n):

    s = 0

    while n != 0:

        r = n % 10

        s = s * 10 + r

        n = n // 10

    return s

 

x = 842

num = number(x)

print(num)

 

X

n

r = n % 10

s = s * 10 + r

num

n!=0

output

842

842

2

0

248

true

248

 

84

4

2

 

true

 

 

8

8

24

 

true

 

 

0

 

248

 

false

 

 

22. Study the following Python code and answer the question below.

 

a.

def series(num):

    for x in range(1, 6):

        print(num, end=" ")

        num = num * 10 + 7

num = 7

series(num)

 

i. How many times does the loop executes in the above program?

The loop executes five times in the above program

 

ii. List all the variables used in the above program.

The variables used in the above program are num and x.

 

iii. List the operators used in the above program.

The operators used in the above program are =, * and +,

 

iv. Name the loop used in the above program.

The loop used in the above program is for loop.

 

v. Name the user defined function in the above program.

The user defined function in the above program is series.

 

 

b.

def sum_digit(n):

    s = 0

    while n != 0:

        r = n % 10

        s = s + r

        n = n // 10

    return s

 

x = int(input("Enter a number: "))

a = sum_digit(x)

print("Sum of individual digits is:", a)

 

i. Name the loop used in the above program.

The loop used in the above program is while loop.

 

ii. At what value of n will the loop terminates?

Loop will terminate when the value of n is 0.

 

iii. How many times will the loop execute if the input number is 54399

Loop will execute for four times if the input number is 5439.

 

iv. List the formal parameter and actual parameter used in the above progr

The formal parameter is n and actual parameter is x.