In this program, we will find the sum of the first and last digits of a number.
Steps to solve the program
- Take a number as input and convert it into a string using str() and store it in another variable.
- Create a variable total and assign its value equal to 0.
- Use for loop with the range function to find the first and last digit of the number logically.
- Add only those number to the total variable.
- Print the output
num = 2665
str1 = str(num)
total= 0
for i in range(len(str1)):
if i == 0:
total += int(str1[i])
elif i == len(str1)-1:
total += int(str1[i])
print("Sum of first and last number: ",total)
Output :
Sum of first and last number: 7