# Write a python function that takes in two numbers and returns their HCF - Python

Categories:
Viewed: 3 - Published at: a few seconds ago

### Write a python function that takes in two numbers and returns their HCF

``````def hcf(num1, num2):
smaller = num1 if num1 < num2 else num2
for i in range(1, smaller+1):
if (num1 % i == 0) and (num2 % i == 0):
hcf = i
return hcf
``````

### Write a python function that takes in two numbers and returns their LCM

``````def lcm(num1, num2):
bigger = num1 if num1 > num2 else num2
while True:
if (bigger % num1 == 0) and (bigger % num2 == 0):
break
bigger += 1
return bigger
``````

### Write a recursive python function to calculate sum of natural numbers upto n, where n is an argument

``````def recursive_sum(n):
if n <= 1:
return n
else:
return n + recursive_sum(n-1)
``````

### Write a python function that deletes the last element of a list and returns the list and the deleted element

``````def delete_last_element(list_to_be_processed):
deleted_element = list_to_be_processed.pop()
return list_to_be_processed, deleted_element
``````

### Write a python function that takes in a list and returns a list containing the squares of the elements of the input list

``````def square_list_elements(list_to_be_squared):
return list( map(lambda x: x**2, list_to_be_squared) )
``````

### Write a python function that finds square roots of a given number, if the square root is an integer, else returns the message "Error - the square root is not an integer"

``````def find_integer_square_roots(num):
found = False
for k in range(1, (num//2)+1):
if ((k**2)==num):
found = True
break
return "Error - the square root is not an integer"
return -k, k
``````

### Write a python program that prints out natural numbers less than or equal to the given number using a while loop

``````input_num = 27
while input_num:
print(input_num)
input_num -= 1
``````

### Write a python function that takes two numbers. The function divides the first number by the second and returns the answer. The function returns None, if the second number is 0

``````def divide(num1, num2):
if num2 == 0:
return
else:
return num1 / num2
``````

### Write a python program uses else with for loop

``````seq = "abcde"
for k in seq:
if k == "f":
break
else: