Recently I need to add some of this kind of functionality in my on-going project and for that, I wrote some simple method to returning the count of prime Numbers contain for given input number…. so I just want to have this snippet in front of you… Hope it will help as.. as it is simple but while implementing it kills our brain surely 🙂

  1. static int getNumberOfPrimes(int N) {
  2. int count = 0;
  3. bool res = false;
  4. for(int i=2;i<=N;i++)
  5. {
  6. res = false;
  7. for(int j=2;j<=i;j++)
  8. {
  9. if(i%j == 0 && i!=j)
  10. {
  11. res = true;
  12. }
  13. }
  14. if(!res)
  15. count++;
  16. }
  17. return count;
  18. }
static int getNumberOfPrimes(int N) {
     
    int count = 0;
    bool res = false;
    for(int i=2;i<=N;i++)
    {
        res = false;
        for(int j=2;j<=i;j++)
        {
            if(i%j == 0 && i!=j)
            {
                res = true;
            }
        }
        if(!res)
            count++;
    }
     
    return count;
 
}

Hope it will help you 🙂