In this Python Program, we will learn how to find the first digit of a number.
Here is the source code of the program to find the first digit of a number.
Python Program to find First Digit of a Number using While Loop
# Python Program to find First Digit of a Number using While Loop
# To Take Input From the User
number = int(input("Enter any Number: "))
first_digit = number
while (first_digit >= 10):
first_digit = first_digit // 10
print("The First Digit from a Given Number {0} = {1}".format(number, first_digit))
Enter any Number: 56546
The First Digit from a Given Number 56546 = 5
Enter any Number: 785968520
The First Digit from a Given Number 785968520 = 7
In this program, first, we import the math
module and use the math.log10()
function and math.pow()
function.
Python Program to find First Digit of a Number using Built-in Functions
# Python Program to find the First Digit of a Number using Built-in Functions
# Import Module
import math
# To Take the Input from the User
number = int(input("Enter any Number: "))
count = int(math.log10(number))
first_digit = number // math.pow(10, count)
#print("The Total Number of Digit in a Given Number {0} = {1}".format(number, count))
print("The First Digit from a Given Number {0} = {1}".format(number, first_digit))
Enter any Number: 466431
The First Digit from a Given Number 466431 = 4.0
Enter any Number: 15246564
The First Digit from a Given Number 15246564 = 1.0
Comments