Question #173932

First and Last Digits
Given two integers 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 of 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 numbers are M is 5 and 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

Expert's answer

# simple program First and Last Digits
# int(str(number)[0]) -its first digital number
# number % 10 - -its last digital number
def main():
    n = int(input('Enter a positive integer N. '))
    m = int(input('Enter a positive integer M. '))
    total = 0 
    for i in range (n, m+1):
        if int(str(i)[0]) == i % 10:
            total += 1
    print (f'Found {total} numbers corresponding to the condition')

if __name__ == "__main__":
    main()
LATEST TUTORIALS
APPROVED BY CLIENTS