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

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



20 views0 comments

Recent Posts

See All

Binary string reduction in Python

Asked: Sudeep |Category: Python x=int(input("Enter the value of x : ")) for i in range(x): n=int(input("Enter the value of n : ")) y=input("Enter the value of y : ") l=list(y) k=0

"Believe you can and you're halfway there"