Find given number is a digit in a number using Python

Get a number and a digit and find whether the digit is occurred in the number.

Sample Input 1:

Enter the number: 234

Enter the Digit : 4

Sample Output 1:

4 is occurred in 234

Sample Input 2:

Enter the number: 974

Enter the Digit : 3

Sample Output 2:

3 is not occurred in 974


Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#write your code here

Program or Solution

				
				
					

num = int(input("Enter the number"))

digit = int(input("Enter a Digit"))

count = 0

n = num

while n != 0:

    rem = n % 10

    if rem == digit:

       print("{} is occured in {}".format(digit,num))

       break

    n = n // 10

else:

    print("{} is not occured in {}".format(digit,num))

Program Explanation

calculate the remainder of a number by doing number % 10

check remainder is equal to digit, if it is equal print digit occurred, else divide number by the 10 and repeat the above steps till number becomes zero.

Comments