Sai Stuff to Developers

April 23, 2013

Finding count of Prime Numbers contain for a given Number

Filed under: DotNet,OOPS Languages — tosaik @ 11:44 am
Tags: , , , ,

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 surly 🙂


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 for you 🙂

Create a free website or blog at WordPress.com.