Sum of the first and last digits of a number

In this program, we will find the sum of the first and last digits of a number.

Steps to solve the program
  1. Take a number as input and convert it into a string using str() and store it in another variable.
  2. Create a variable total and assign its value equal to 0.
  3. Use for loop with the range function to find the first and last digit of the number logically.
  4. Add only those number to the total variable.
  5. 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
				
			

find the first and last digits of a number

calculate the sum of digits of a number

Leave a Comment