Numbers containing 0's from 1 to N

PROBLEM :

Efficiently count how many integers from 1 to N contains 0's as a digit.

Input:
First line of the input contains the number of test cases T. Each line of test case contains the integer N.

Output:
Number of integers that contain 0 as a digit are printed.

Constraints:
1 <=T<= 100
1 <=N<= 10000

Example:
Input:
3
100
987
20

Output:
10
179
2

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int count_zeroes(int ) ;
int main()
 {
    int t,no ;
    cin>>t ;
    while(t--)
    {
        cin>>no ;
        cout<<count_zeroes(no)<<endl ;
    }
   
return 0;
}

int count_zeroes(int no)
{
    int i,count ;
    count=0 ;
   
    for(i=1;i<=no;i++)
    {
        if((i>1000)&&((i/10)%10==0||(i/100)%10==0))
            count++;
        else if(i>10&&(i/10)%10==0)
            count++ ;
        else if(i%10==0)
            count++ ;
    }
    return count ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )