0% found this document useful (0 votes)
19 views8 pages

Chapter 3 12

The document contains a series of programming exercises focused on creating functions in Python. Each question includes a specific task, such as converting currency, calculating volume, generating random numbers, and checking string lengths, along with sample solutions and outputs. The exercises emphasize the use of parameters, default values, and various return types in function definitions.

Uploaded by

chikbhandary2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Chapter 3 12

The document contains a series of programming exercises focused on creating functions in Python. Each question includes a specific task, such as converting currency, calculating volume, generating random numbers, and checking string lengths, along with sample solutions and outputs. The exercises emphasize the use of parameters, default values, and various return types in function definitions.

Uploaded by

chikbhandary2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Type C: Programming Practice/Knowledge based

Questions

Question 1

Write a function that takes amount-in-dollars and dollar-to-rupee


conversion price; it then returns the amount converted to rupees.
Create the function in both void and non-void forms.

Solution

def convert_dollars_to_rupees(amount_in_dollars, conversion_rate):


amount_in_rupees = amount_in_dollars * conversion_rate
return amount_in_rupees

def convert_dollars_to_rupees_void(amount_in_dollars,
conversion_rate):
amount_in_rupees = amount_in_dollars * conversion_rate
print("Amount in rupees:", amount_in_rupees)

amount = float(input("Enter amount in dollars "))


conversion_rate = float(input("Enter conversion rate "))

# Non-void function call


converted_amount = convert_dollars_to_rupees(amount, conversion_rate)
print("Converted amount (non-void function):", converted_amount)

# Void function call


convert_dollars_to_rupees_void(amount, conversion_rate)

Output

Enter amount in dollars 50


Enter conversion rate 74.5
Converted amount (non-void function): 3725.0
Amount in rupees: 3725.0

Enter amount in dollars 100


Enter conversion rate 75
Converted amount (non-void function): 7500.0
Amount in rupees: 7500.0

Question 2

Write a function to calculate volume of a box with appropriate


default values for its parameters. Your function should have the
following input parameters :

(a) length of box ;

(b) width of box ;

(c) height of box.

Test it by writing complete program to invoke it.

Solution

def calculate_volume(length = 5, width = 3, height = 2):


return length * width * height

default_volume = calculate_volume()
print("Volume of the box with default values:", default_volume)

v = calculate_volume(10, 7, 15)
print("Volume of the box with default values:", v)

a = calculate_volume(length = 23, height = 6)


print("Volume of the box with default values:", a)

b = calculate_volume(width = 19)
print("Volume of the box with default values:", b)

Output

Volume of the box with default values: 30


Volume of the box with default values: 1050
Volume of the box with default values: 414
Volume of the box with default values: 190

Question 3
Write a program to have following functions :

(i) a function that takes a number as argument and calculates cube


for it. The function does not return a value. If there is no value
passed to the function in function call, the function should calculate
cube of 2.

(ii) a function that takes two char arguments and returns True if
both the arguments are equal otherwise False.

Test both these functions by giving appropriate function call


statements.

Solution

# Function to calculate cube of a number


def calculate_cube(number = 2):
cube = number ** 3
print("Cube of", number, "is", cube)

# Function to check if two characters are equal


def check_equal_chars(char1, char2):
return char1 == char2

calculate_cube(3)
calculate_cube()

char1 = 'a'
char2 = 'b'
print("Characters are equal:", check_equal_chars(char1, char1))
print("Characters are equal:", check_equal_chars(char1, char2))

Output

Cube of 3 is 27
Cube of 2 is 8
Characters are equal: True
Characters are equal: False

Question 4
Write a function that receives two numbers and generates a
random number from that range. Using this function, the main
program should be able to print three numbers randomly.

Solution

import random

def generate_random_number(num1, num2):


low = min(num1, num2)
high = max(num1, num2)
return random.randint(low, high)

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


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

for i in range(3):
random_num = generate_random_number(num1, num2)
print("Random number between", num1, "and", num2, ":",
random_num)

Output

Enter the first number: 2


Enter the second number: 78
Random number between 2 and 78 : 77
Random number between 2 and 78 : 43
Random number between 2 and 78 : 52

Enter the first number: 100


Enter the second number: 599
Random number between 100 and 599 : 187
Random number between 100 and 599 : 404
Random number between 100 and 599 : 451

Question 5

Write a function that receives two string arguments and checks


whether they are same-length strings (returns True in this case
otherwise False).

Solution
def same_length_strings(str1, str2):
return len(str1) == len(str2)

s1 = "hello"
s2 = "world"
s3 = "python"

print(same_length_strings(s1, s2))
print(same_length_strings(s1, s3))

Output

True
False

Question 6

Write a function namely nthRoot( ) that receives two parameters x


and n and returns nth root of x i.e., x^(1/n).
The default value of n is 2.

Solution

def nthRoot(x, n = 2):


return x ** (1/n)

x = int(input("Enter the value of x:"))


n = int(input("Enter the value of n:"))

result = nthRoot(x, n)
print("The", n, "th root of", x, "is:", result)

default_result = nthRoot(x)
print("The square root of", x, "is:", default_result)

Output

Enter the value of x:36


Enter the value of n:6
The 6 th root of 36 is: 1.8171205928321397
The square root of 36 is: 6.0
Question 7

Write a function that takes a number n and then returns a randomly


generated number having exactly n digits (not starting with zero)
e.g., if n is 2 then function can randomly return a number 10-99 but
07, 02 etc. are not valid two digit numbers.

Solution

import random
def generate_number(n):
lower_bound = 10 ** (n - 1)
upper_bound = (10 ** n) - 1
return random.randint(lower_bound, upper_bound)

n = int(input("Enter the value of n:"))


random_number = generate_number(n)
print("Random number:", random_number)

Output

Enter the value of n:2


Random number: 10

Enter the value of n:2


Random number: 50

Enter the value of n:3


Random number: 899

Enter the value of n:4


Random number: 1204

Question 8

Write a function that takes two numbers and returns the number
that has minimum one's digit.

[For example, if numbers passed are 491 and 278, then the function
will return 491 because it has got minimum one's digit out of two
given numbers (491's 1 is < 278's 8)].
Solution

def min_ones_digit(num1, num2):


ones_digit_num1 = num1 % 10
ones_digit_num2 = num2 % 10
if ones_digit_num1 < ones_digit_num2:
return num1
else:
return num2

num1 = int(input("Enter first number:"))


num2 = int(input("Enter second number:"))
result = min_ones_digit(num1, num2)
print("Number with minimum one's digit:", result)

Output

Enter first number:491


Enter second number:278
Number with minimum one's digit: 491

Enter first number:543


Enter second number:765
Number with minimum one's digit: 543

Question 9

Write a program that generates a series using a function which


takes first and last values of the series and then generates four
terms that are equidistant e.g., if two numbers passed are 1 and 7
then function returns 1 3 5 7.

Solution

def generate_series(first, last):


step = (last - first) // 3
series = [first, first + step, first + 2 * step, last]
return series

first_value = int(input("Enter first value:"))


last_value = int(input("Enter last value:"))
result_series = generate_series(first_value, last_value)
print("Generated Series:", result_series)

Output

Enter first value:1


Enter last value:7
Generated Series: [1, 3, 5, 7]

Enter first value:10


Enter last value:25
Generated Series: [10, 15, 20, 25]

You might also like