Over a million developers have joined DZone.

Find if number is prime - C#

·
Find if number is prime - C#
public bool IsPrime(int num)
        {
            bool _isPrime = true;

            if (num % 2 == 0) return false;

            for (int i = 3; i <= Convert.ToInt32(Math.Sqrt(num)); i = i + 2)
            {
                if (num % i == 0)
                {
                    _isPrime = false;
                    break;
                }
            }
            return _isPrime;
        }
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}