In this article I try to solve a Python program where you need to count the number of digits present in the number using a while loop.
In order to solve the above program do you need to Perform true division or integer division by 10 on the given number. True division by 10 will trim the last digit from the number.
Write a Python Program to Count The Total Number of Digits in a Number Using a While Loop
Ask the user to enter a number using input function call and convert it into integer using type conversion.
Take a temporary variable and store a copy of the number. also initialize the digit count variable to 0.
number = int(input("Enter a Number : "))
temp = number
count = 0
Use of while loop until the number becomes zero and perform the true division by 10 and also increment the digit count variable by one.
while temp != 0:
temp = temp // 10
count = count + 1
Once the follow completes digit count variable will store The Count of total number of digits and finally print the total number of digits present in the number using a print function call
print("Total Number of Digits in a Number is : ", count)
Final program:
number = int(input("Enter a Number : "))
temp = number
count = 0
while temp != 0:
temp = temp // 10
count = count + 1
print("Total Number of Digits in a Number is : ", count)
Output:
Enter a Number : 23456
Total Number of Digits in a Number is : 5
Output:
Enter a Number : 123456789
Total Number of Digits in a Number is : 9
Conclusion: Try to run the program and comment down below if you have any queries or concerns in the comment section
0 Comments