Program to count the Occurrence of a Digit in a given Number in Python

Occurrence of a Digit in a given Number in Python

 
 
Here, in this page we will discuss the program to find the Occurrence of a Digit in a given Number in Python programming language. We will discuss different methods to solve this problem.
number of times x digit occurs in python

Method Discussed :

  • Method 1 : Using loops
  • Method 2 : Using String

 

Method 1 :

  • Declare variable count that will count the required number of occurrences
  • Take a while loop. 
    • Check if(n%10 ==d)
    • If yes, then increment the value of count by 1.
    • Set, n=n//10
  • Print the value of count.

Method 1 : Code in Python

#Write a program to print the Occurrence of a Digit in a given Number in Python
def countOccurrances(n, d):
    count = 0
 
    # Loop to find the digits of N
    while (n > 0):
 
        # check if the digit is D
        if(n % 10 == d):
            count = count + 1
  
        n = n // 10
 
    return count
 
# Driver code
d = 2
n = 828282
print(countOccurrances(n, d))

Output:

3

Method 2 :

In this method we will convert the number and digit into string, and then count them.

Method 2 : Code in Python

#Write a program to print the Occurrence of a Digit in a given Number in Python
#Write a program to print the Occurrence of a Digit in a given Number in Python

Number = 828282
Digit = 2

#initialize Strings
String1 = str()
String2 = str()

#typecast int to str
String1 = str(Number)
String2 = str(Digit)

#count and print the occurrence
#Count function will return int value 
#so change it's type to string and concatenate it
print(String1.count(String2))


Output:

3

9 comments on “Program to count the Occurrence of a Digit in a given Number in Python”


  • PRIYA

    def count_digit(number,digit):
    count=0
    for i in str(number):
    if i==str(digit):
    count+=1
    print(f”Count of {digit} is {count}”)

    count_digit(98219,9)


  • Pulla

    n,x=[x for x in input(“Enter the number and digit: “).split()]
    print(n.count(x))


  • Arun

    a=input(“enter the value :”)
    b=input(“enter the value of digit:”)
    print(a.count(b))


  • DHANANJAY

    ———————————————
    n = str(input(‘Enter Number: ‘))
    k = str(input(‘Enter the digit: ‘))
    print(“Digit count is: “,n.count(k))


  • Abhishek

    n=input(“Enter a number:”)
    m=input(“Enter Digit to check:”)
    list1=list(n)
    list2=[]

    for i in range(len(list1)):
    if(list1[i]==m):
    list2.append(list1[i])

    print(len(list2))


  • Om Prakash

    Code in Java :
    import java.util.Scanner;
    public class Frequency_of_Digit
    { public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    System.out.print(“Enter number: “);
    String number = sc.nextLine();
    System.out.print(“Enter digit: “);
    char digit = sc.nextLine().charAt(0);
    sc.close();
    int count=0;
    for(int i=0; i<number.length(); i++)
    if(number.charAt(i)==digit)
    count++;
    System.out.println(count);
    }
    }


    • Arun

      a=input(“enter the value :”)
      b=input(“enter the value of digit:”)
      print(a.count(b))


  • placement

    program in c given below

    #include
    void main()
    {
    int n,d,count=0,r;
    printf(“enter the number”);
    scanf(“%d”,&n);
    printf(“enter digit”);
    scanf(“%d”,&d);
    while(n>0)
    {
    r=n%10;
    if(r==d)
    {
    count++;
    }
    n=n/10;
    }
    printf(“%d occurs %d times”,d,count);
    }


  • shailendra

    #include

    int main()
    {
    int n,count=0;
    printf(“enter last number:”);
    scanf(“%d”,&n);
    for(int i=1;i0){
    if(m%10==3){
    count++;
    }
    m=m/10;

    }
    }
    printf(“%d”,count);
    return 0;
    }