Solve the python program
Attachments:
Answers
Answered by
6
This problem is about finding the last digit of a number and printing it along the input number, we are asked to create a function to do this task.
Required program :-
def last(n):
n = str(n)
k = len(n)
print(f"The number is {n} and it's last digit is {n[k-1]}")
Explanation :-
- Firstly create a function named last (or according to your choice).
- Now typecast the input value to string form.
- Now find the length of the input string by using len() function.
- We know that index ranging starts from 0 but len() function starts from 1, we have to subtract 1 from the length of string (TypeCasted number) in order to print the last value of string.
- Now using print function print the last digit of number along with the number as mentioned in the question in function.
Let's check our program :-
>>> last(98)
8
>>> last(5437)
7
>>> last(2314350)
0
Similar questions