Skip to main content

C# Programming : Primes less than 100

Here is a ready made function in C# that lists all primes greater than 2 and  less than 100. 


 protected void GetPrimes()
        {
            
            int i, j;
            i = 2;
            j = 100;
            StringBuilder sb = new StringBuilder();

            while (i < 100)
            {

                if ((j % i) == 0) //not a prime
                {
                    
                    j = j - 1;
                    i = 2;
                    continue;
                }
                else
                {
                    i = i + 1;
                }

                if (i==j) { sb.Append(j.ToString()+ ", "); }              
            }

            MessageBox.Show(sb.ToString());
        }//end

Comments