Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Looping Statements

Count how many times a digit occurs in a number - Python Program

Example 1 :

# Count digit frequency num = input("Enter a number: ") digit = input("Enter digit to count: ") count = 0 for ch in num: if ch == digit: count += 1 print(f"Digit '{digit}' occurs {count} times in {num}.")

Output

 
OUTPUT  :
Enter a number: 1123451141
Enter digit to count: 1
Digit '1' occurs 5 times in 1123451141. 
 

Explanation

  • Takes number as string to allow easy iteration.
  • Compares each character with the given digit.
  • Increments count if match found.

Example 2 : Advanced Program

def count_digit_occurrences(number, digit_to_count): """ Counts how many times a specific digit occurs in a given number. Args: number (int): The integer in which to search for the digit. digit_to_count (int): The digit to count occurrences of. Returns: int: The number of times the digit_to_count appears in the number. """ if not isinstance(number, int) or not isinstance(digit_to_count, int): raise TypeError("Both 'number' and 'digit_to_count' must be integers.") if not 0 <= digit_to_count <= 9: raise ValueError("'digit_to_count' must be a single digit (0-9).") count = 0 # Convert the number to a string to easily iterate through its digits num_str = str(number) # Convert the digit to count to a string for comparison digit_str = str(digit_to_count) for char in num_str: if char == digit_str: count += 1 return count # Example Usage and Output if __name__ == "__main__": num = 1232142 digit = 2 occurrences = count_digit_occurrences(num, digit) print(f"The digit {digit} appears {occurrences} times in the number {num}.") num2 = 78907 digit2 = 7 occurrences2 = count_digit_occurrences(num2, digit2) print(f"The digit {digit2} appears {occurrences2} times in the number {num2}.") num3 = 5555 digit3 = 5 occurrences3 = count_digit_occurrences(num3, digit3) print(f"The digit {digit3} appears {occurrences3} times in the number {num3}.") num4 = 100 digit4 = 0 occurrences4 = count_digit_occurrences(num4, digit4) print(f"The digit {digit4} appears {occurrences4} times in the number {num4}.")

Output

 
OUTPUT  :
The digit 2 appears 3 times in the number 1232142.
The digit 7 appears 2 times in the number 78907.
The digit 5 appears 4 times in the number 5555.
The digit 0 appears 2 times in the number 100.
 

Explanation

count_digit_occurrences(number, digit_to_count) function:

  • This function takes two integer arguments: number(the integer to analyze) and digit_to_count (the specific digit whose occurrences are to be counted).
  • Input Validation:It includes checks to ensure both inputs are integers and that digit_to_count is a single digit between 0 and 9.
  • Conversion to String:The core idea is to convert the number into a string (num_str). This allows for easy iteration over each character (which represents a digit) within the number. The digit_to_count is also converted to a string (digit_str) for direct comparison.
  • Iteration and Counting:The program then iterates through each char in num_str. If a char matches digit_str, a count variable is incremented.
  • Return Value:Finally, the function returns the count, representing the total occurrences of the specified digit.