[Type C] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

8. Write a function that takes two numbers and returns the number that has the minimum one’s digit. For example, if numbers passed are 491 and 278, then the function will return 491 because it has the minimum one’s digit out of the two given numbers (491’s 1 is < 278’s 8).

Short Answer: The function compares the one’s digit of two given numbers and returns the number with the minimum one’s digit.

Python
def min_ones_digit(num1, num2):
    return num1 if num1 % 10 < num2 % 10 else num2

# Test
print(min_ones_digit(491, 278))  # 491

Explanation:

  • The function min_ones_digit compares the one’s digit of two numbers using the modulo operator %.
  • It returns the number with the minimum one’s digit.
  • The test case demonstrates how the function works with two example numbers.
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *