First and Last Digits in Python | Assignment Expert

Problem Statement:

In First and Last Digits in Python, we are given two positive numbers, we need to find the count of those numbers which have the same first and last digit. Also, if the number is below 10 it will also be counted. For example, 1, 2, 3, 4, … ,9. will be counted.

Code for First and Last Digits in Python:

n1 = 5
n2 = 203
count = 0
for i in range(n1, n2 + 1):
    if i<10:
        count += 1
    elif 10<i<100:
        if i%11==0:
            count += 1
    else:
        i = str(i)
        first_digit = i[0]
        last_digit = i[-1]
        if first_digit==last_digit:
            count += 1
print(count)

Output:

First and Last Digits in Python

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you