Write a python program to count the occurrence of a element

Write a python program to count the occurrence of a element 

Problem:  in this first line you are required to take the input of an integer number, and in the second line you are required to input the target element 

You are required to print the number of time target element occurred in the integer

Test Case :

Input : 734139

               3

Output :  

               2 

Explanation : 

               3 occurred 2 times in the integer.

Code in Python

n=int(input())

x=int(input())

c=0

for i in str(n):

    if int(i)==x:

        c+=1

print(c)

Check out the code

As we cannot iterate over a integer we have converted it into a string

then converted it into a integer while comparing it with the no to found

and incremented the count by every time no is found


Post a Comment

0 Comments