Count the numbers

PROBLEM :

Given a number N, count the numbers from 1 to N which comprise of digits, only in set 1, 2, 3, 4 and 5.

Input:

The first line of input contains an integer T denoting the number of test cases.Then T test cases follow. Each test case consist of one line. Each line of each test case is N, where N is the range from 1 to N.

Output:

Print the count of numbers in the given range from 1 to N.

Constraints:

1 = T = 100
1 = N = 1000

Example:

Input:
2
100
10
Output:
30
5


Explanation:

When n is 20 then answer is 10 because 1 2 3 4 5 11 12 13 14 15 are only in given set. 16 is not beause 6 is not in given set, only 1 2 3 4 5 in set.

--------------------------------------------------------------------------------
Simple C++ Code :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int call(int) ;
int main()
 {
int test,no,i,r,count  ;
cin>>test ;
while(test--)
{
   cin>>no ;
   count=0 ;
   r=0 ;
   for(i=1;i<=no;i++)
   {
       r=call(i) ;
     
       if(r==1)
           count++ ;
       else
        continue ;
         
   }
   cout<<count<<endl ;
}
return 0;
}
int call(int no)
{
    int r ;
lable :
    while(no!=0)
    {
    r=no%10 ;
   
        if(r==1||r==2||r==3||r==4||r==5)
        {
            no=no/10 ;
            goto lable ;
        }
        else
        return 0 ;
    }
    return 1 ;
}

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

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 )