First and last digits
Given two numbers M and N, write a program to count of the numbers which have the same first and last digits in the given range M to N(inclusive M and N)
Input
the first line of input will contain a positive integer M.
the second line of input will contain a positive integer N.
output
The output should be an integer denoting the count of the numbers in the range which meet the given condition.
Explanation
For example, if the given number are M is 5and N is 30, the numbers which have the first and last digit equal in the given range(5,6,....29,30) are 5 6 7 8 9 11 and 22. so the output should be 7.
sample input 1
5
30
sample output 1
7
sample input 2
1
10
sample output 2
9
m = int(input("Enter the START value of the range (M): "))
n = int(input("Enter the END value of the range (N): "))
count = 0
for i in range(m, n + 1):
if i < 10:
count += 1
continue
else:
if i // 10 == i % 10 or i // 100 == i % 100 or i // 1000 == i % 1000:
count += 1
print("Counts of the numbers which have the same first and last digits" + str(count))
Comments
Leave a comment