 Explore
Placement Prep Edit Edit

# Find the number of times digit 3 occurs in each and every number from 0 to n

Published on 10 Mar 2020

Program to find the number of times digit 3 occurs in each and every number from 0 to n is discussed here. Given a number n as input, count the number of 3s occurring in all the numbers from 0 to n.

For example,

Input: 100

Output: 20

Total number of 3s that appear from numbers 0 to 100 are {3, 13, 23, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 53, 63, 73, 83, 93}

## Algorithm to find the number of times digit 3 occurs in each and every number

1. Input the number n from the user.
2. Initialize count = 0.
3. Repeat for all numbers from o to n.
4. Find if num % 10 == 3
5. Increment count
6. Divide the number by 10 and from step 4.
7. Return count.

Program to find the number of times digit 3 occurs in each and every number from 0 to n is given below.

C
C++
Java
Python 3

Output
Enter the end value : 100
Total occurrences of 3 from 0 to 100 is 20

Time complexity: O(n)

Recommended Programs  