Python Practice › Loop Programs › Program 21

Sum of the first and last digits

EasyLoop Programs · 21 of 90

Write a Python program to find the sum of the first and last digits of a number.

Advertisement
Input
74351
Output
7 + 1 = 8

Approach

  1. Find both digits as in the previous program and add them.

Solution

Python
n = 74351last = n % 10first = nwhile first >= 10:    first //= 10print(first, "+", last, "=", first + last)
Output
7 + 1 = 8

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Leave a Comment