Coding Assignments ( Python Functions )- Problems and Solutions
This article contains coding exercises related to python functions.
NOTE : The solutions are given at the last of this article. But try to write your code before taking the help of the solutions.
Problem 1 (Factorial)
Statement
Write a function to compute the factorial of a given number.
Explanation
def factorial(n):
Complete the Body
a = factorial(4)
print(a) # 24
b = factorial(5)
print(b) # 120
Problem 2 (Sum of Digits)
Statement
Write a function sum_of_digits that takes an integer value as an argument and returns sum of digits of given number.
Explanation
def sum_of_digits(n):
Complete the Body
a = sum_of_digits(123)
print(a) # 6
b = sum_of_digits(25)
print(b) # 7
Problem 3 (Prime Number)
Write a function is_prime(n) : It tests if n is a prime number (True) or not (False).
Explanation
def is_prime(n):
Complete the Body
a = is_prime(23)
print(a) # True
b = is_prime(15)
print(b) # False
Problem 4 (Highest Common factor or greatest common divisor)
Write a function hcf which takes 2 arguments and returns the greatest common divisor of these two numbers.
Explanation
def hcf(a,b):
Complete the Body
a = hcf(12,16)
print(a) # 4
Problem 5 (Lowest Common Multiple)
Write a function lcm which takes 2 arguments and returns the lowest common multiple of these two numbers.
Explanation
def lcm(a,b):
Complete the Body
y = lcm(12,16)
print(y) # 48
Problem 6 (Armstrong number)
Statement
Write a function is_armstrong(n) : It tests if n is a Armstrong number (True) or not (False).
Note - Use google to know Armstrong number
def is_armstrong(n):
Complete the Body
a = is_armstrong(153)
print(a) # True
b = is_armstrong(234)
print(b) # False
Solutions
1) Factorial - https://replit.com/@rhtm123/pythonfunction01
2) Sum of digits of a number - https://replit.com/@rhtm123/pythonfunction02
3) Prime number - https://replit.com/@rhtm123/pythonfunction03
4) Highest Common factor (HCF) or greatest common divisor (GCD) - https://replit.com/@rhtm123/pythonfunction04
5) Lowest Common Multiple (LCM) - https://replit.com/@rhtm123/pythonfunction05
6) Armstrong number - https://replit.com/@rhtm123/pythonfunction06