![]() |
|
[Tut] How to Find Number of Digits in an Integer? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Find Number of Digits in an Integer? (/thread-99621.html) |
[Tut] How to Find Number of Digits in an Integer? - xSicKxBot - 06-23-2022 How to Find Number of Digits in an Integer? Rate this post To find the number of digits in an integer you can use one of the following methods: (1) Use Iteration (2) Use str()+len() functions (3) Use int(math.log10(x)) +1 (4) Use Recursion [b]Problem Formulation[/b]
Test Cases: Code: Input:
num = 123
Output: 3
=========================================
Input:
num = -123
Output: 3
=========================================
Input: num = 0
Output: 1Approach:
Code: Code: num = int(input("Enter an Integer: "))
num = abs(num)
digit_count = 0
if num == 0: print("Number of Digits: ", digit_count)
else: while num != 0: num //= 10 digit_count += 1 print("Number of Digits: ", digit_count)Output: Code: Test Case 1:
Enter an Integer: 123
Number of Digits: 3 Test Case 2:
Enter an Integer: -123
Number of Digits: 3 Test Case 3:
Enter an Integer: 0
Number of Digits: 1Explanation through tabular dry run: ![]() Readers Digest:Python’s built-in Code: abs(x)Code: xCode: __abs__()Code: -xCode: +xCode: +x[b]Method 2: Using str() and len()[/b] Approach: Convert the given integer to a string using Python’s Code: str()To deal with negative numbers, you can use the Code: abs()Code: Code: num = int(input("Enter an Integer: "))
if num >= 0: digit_count = len(str(num))
else: digit_count = len(str(num)) - 1 # to eliminate the - sign
print("Number of Digits: ", digit_count)Output: Code: Test Case 1:Enter an Integer: 123Number of Digits: 3Test Case 2:Enter an Integer: -123Number of Digits: 3Test Case 3:Enter an Integer: 0Number of Digits: 1Alternate Formulation: Instead of using Code: str(num)Code: num = abs(int(input("Enter an Integer: ")))
digit_count = len('%s'%num)
print("Number of Digits: ", digit_count)Disclaimer: This approach works if the given number is less than 999999999999998. This happens because the float value returned has too many .9s in it which causes the result to round up. Prerequisites: To use the following approach to solve this question, it is essential to have a firm grip on a couple of functions:
Example: ![]() 2. int(x) – It is a built-in function in Python that converts the passed argument x to an integer value. For example, Code: int('24')Code: '24'Code: 24Code: int()Example: ![]() Approach:
Code: Code: import math
num = int(input("Enter an Integer: "))
if num > 0: digit_count = int(math.log10(num))+1
elif num == 0: digit_count = 1
else: digit_count = int(math.log10(-num))+1
print("Number of Digits: ", digit_count)Output: Code: Test Case 1:Enter an Integer: 123Number of Digits: 3Test Case 2:Enter an Integer: -123Number of Digits: 3Test Case 3:Enter an Integer: 0Number of Digits: 1Recursion is a powerful coding technique that allows a function or an algorithm to call itself again and again until a base condition is satisfied. Thus, we can use this technique to solve our question. Code: Code: def count_digits(n): if n < 10: return 1 return 1 + count_digits(n / 10) num = int(input("Enter an Integer: "))
num = abs(num)
print(count_digits(num))Output: Code: Test Case 1:Enter an Integer: 123Number of Digits: 3Test Case 2:Enter an Integer: -123Number of Digits: 3Test Case 3:Enter an Integer: 0Number of Digits: 1Question: Given a string. How will you cont the number of digits, letters, spaces and other characters in the string? Solution: Code: text = 'Python Version 3.0'
digits = sum(x.isdigit() for x in text)
letters = sum(x.isalpha() for x in text)
spaces = sum(x.isspace() for x in text)
others = len(text) - digits - letters - spaces
print(f'No. of Digits = {digits}')
print(f'No. of Letters = {letters}')
print(f'No. of Spaces = {spaces}')
print(f'No. of Other Characters = {others}')Output: Code: No. of Digits = 2
No. of Letters = 13
No. of Spaces = 2
No. of Other Characters = 1Explanation: Check if each character in the given string is a digit or a letter or a space or any other character or not using built-in Python functions. In each case find the cumulative count of each type with the help of the Code: sum()
Conclusion We have discussed as many as four different ways of finding number of digits in an integer. We also solved a similar exercise to enhance our skills. I hope you enjoyed this question and it helped to sharpen your coding skills. Please stay tuned and subscribe for more interesting coding problems. Recommended Read: Coding Interview Questions Recommended: Finxter Computer Science Academy
Join the Web Scraping with BeautifulSoup Masterclass now, and master it by tomorrow!
|