0
340views
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

1 Answer
0
1views

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)

Note:-

  • 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
Please log in to add an answer.