Part of the series

Several example codes

~1 min read • Updated Oct 7, 2025

Program Overview

This Python program reads three numbers from the user:
- First number: start of the range
- Second number: end of the range
- Third number: target digit sum
It then displays all numbers between the first and second whose digit sum equals the third number.


Python Code:


def digit_sum(n: int) -> int:
    return sum(int(d) for d in str(n))

# Read inputs
start = int(input("Enter first number (start of range): "))
end = int(input("Enter second number (end of range): "))
target_sum = int(input("Enter third number (target digit sum): "))

print(f"\nNumbers between {start} and {end} with digit sum equal to {target_sum}:")
for num in range(start, end + 1):
    if digit_sum(num) == target_sum:
        print(num)

Sample Output:


Inputs:  
First number: 5  
Second number: 15  
Third number: 10  

Output:  
Numbers between 5 and 15 with digit sum equal to 10:  
19  
28  
37  
46  
55

Step-by-Step Explanation:

- The digit_sum function calculates the sum of digits of a number
- The program checks each number in the range from start to end
- If the digit sum matches the target, the number is printed


Written & researched by Dr. Shahin Siami