Count All 2s between 0 to n

Count All 2s between 0 to n.

Algorithm:
  • Iterate from 2 to n
    • For each number count number 2s present in it
    • Update total_2s_counter
  • Return total_2s_counter

Latest Source Code:
Github: CountAll2s.java


Output:

All 2's till n=2: 1
All 2's till n=4: 1
All 2's till n=12: 2
All 2's till n=22: 6
All 2's till n=32: 14
Author: Hrishikesh Mishra