Mental Approach to solving prime checking? [closed]

using System;

class Prime_Screening
    {
    bool prime;
    static void Main()
        {
        Console.WriteLine("Prime numbers x, where: 0 < x < 100");

            for (int num= 2; num<= 100; num++)
            {

                prime = True;
                for (int div= 2; div<= Math.Sqrt(num); div++)
                    if(num % div== 0)
                        prime = False;
                if (prime)
                    Console.Write(num.ToString() + ", ");
            }
        } 
    }

The way this works is pretty simple:

It checks if any of the divisors are a factor of the number at hand and if so sets the variable prime to false. Otherwise, prime remains true and the number gets printed.

Browse More Popular Posts

Leave a Comment