Python Program to Find Sum of First & Last Digit of Number
This Python program calculates sum of first and last digit of a given number.
In this program, we first read number
from user. Then we reverse it using [::-1]
. After reversing, we convert both number
& reverse
to integer using int()
function. first_digit
is obtained by reverse%10
and last_digit
is obtained by number%10
. Finally we calculate sum of first & last digit to get final result.
Python Source Code: Sum of First & Last Digit
# Sum of first & last digit
# Reading number
number = input("Enter number: ")
# Reversing number
reverse = number[::-1]
# Converting number and its reverse to integer
number = int(number)
reverse = int(reverse)
# Finding first digit
first_digit = reverse % 10
# Finding last digit
last_digit = number % 10
# Finding sum
total_sum = first_digit + last_digit
# Displaying sum
print("Sum of first & last digit of %d is %d" %(number, total_sum))
Output
Enter number: 99975 Sum of first & last digit of 99975 is 14