Python: Sum of the last digit of first number and the last digit of second number equal to the last digit of third number
Python Basic - 1: Exercise-108 with Solution
Write a Python program that takes three integers and checks whether the sum of the last digit of the first number and the last digit of the second number equals the last digit of the third number.
Sample Solution:
Python Code:
# Define a function named check_last_digit that checks if the last digits of the sum of x and z are equal to the last digit of y.
def check_last_digit(x, y, z):
# Convert the sum of x and z to a string, extract the last digit, and compare it with the last digit of y.
return str(x + z)[-1] == str(y)[-1]
# Test the function with different values for x, y, and z and print the results.
# Test case 1
print(check_last_digit(12, 26, 44))
# Test case 2
print(check_last_digit(145, 122, 1010))
# Test case 3
print(check_last_digit(0, 20, 40))
# Test case 4
print(check_last_digit(1, 22, 40))
# Test case 5
print(check_last_digit(145, 129, 104))
Sample Output:
True False True False True
Explanation:
Here is a breakdown of the above Python code:
- Function definition:
- The code defines a function named "check_last_digit()" that checks if the last digits of the sum of 'x' and 'z' are equal to the last digit of 'y'.
- String Conversion and Last Digit Extraction:
- The function converts the sum of 'x' and 'z' to a string, extracts the last digit using [-1], and compares it with the last digit of 'y'.
Pictorial Presentation:
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to check whether a given number is Oddish or Evenish.
Next: Write a Python program find the indices of all occurrences of a given item in a given list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-108.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics