Previous Post :
Python #17-To print the multiplication Table of a given number
Next Post :
Python #19-Reversing the given number
Counting the number of digits in a number
Eg: 9876543210
This number consists of 10 digits.
Source Code:
number = int (input("Enter the number to count the digits:"))
counter = 0
while (n>0):
counter = counter + 1
number = number / 10
print "The number of digits in the given number is:", counter
Output:
Enter the number to count the digits: 9876543210
The number of digits in the given number is : 10
Explanation:
1. Getting the number from the user keyboard by using the input().
2. Creating a variable name counter initializing it will zero.
3. Now checking whether the given number is greater than Zero using while()
4. If the number is greater than Zero, then the counter value will be incremented by 1
5. Then the given number will divided with the number 10 to remove the last digit from the given number..Like wise the While loop will be running until the number tends to be Zero.
6. Print the Counter value it will give you the number of digits in the given number.
7. End.
Python #17-To print the multiplication Table of a given number
Next Post :
Python #19-Reversing the given number
Counting the number of digits in a number
Eg: 9876543210
This number consists of 10 digits.
Source Code:
number = int (input("Enter the number to count the digits:"))
counter = 0
while (n>0):
counter = counter + 1
number = number / 10
print "The number of digits in the given number is:", counter
Output:
Enter the number to count the digits: 9876543210
The number of digits in the given number is : 10
Explanation:
1. Getting the number from the user keyboard by using the input().
2. Creating a variable name counter initializing it will zero.
3. Now checking whether the given number is greater than Zero using while()
4. If the number is greater than Zero, then the counter value will be incremented by 1
5. Then the given number will divided with the number 10 to remove the last digit from the given number..Like wise the While loop will be running until the number tends to be Zero.
6. Print the Counter value it will give you the number of digits in the given number.
7. End.
Thanks and regards,
Tech bird
Comments