top of page
Click here to go to the home page of AskTheCode.

Python program to get the first digit of an integer - AskTheCode

Team ATC

Updated: Mar 9, 2021

Python Programming | Find first digit of an integer in Python | Ask The Code

 

Problem:

First Digit of an Integer.

We already saw how to find the last digit of a number. Let's proceed ahead with finding the first digit. Given a four-digit number N, find its first digit. There will be no leading zeros in the integer N.


Input:

The first line contains an integer T denoting the number of test cases.

Each test case contains one integer N.


Output:

For each test case on a new line, print the first digit of N.


Constraints:

  • 1<=T<=10

  • 1000<=N<10000

Example Input:

2

1234

4567


Example Output:

1

4

 

Code:


##Python Program To Find The First Digit Of An Integer

#Function to get the first digit
def firstdigit(num):
    while num != 0:
        val = num
        num //= 10
    return val

t = int(input())
while t > 0:
    n = int(input())
    f_d = firstdigit(n)
    print(f_d)
    t -= 1

Recent Posts

See All

Comments


bottom of page