jrliv
1/2/2017 - 4:22 PM

Check if a number is prime. From Fundamentals of Computer Programming with C# http://www.introprogramming.info/wp-content/uploads/2013/07/Bo

int divider = 2;
bool prime = true;

Console.Write("Enter a positive number: " );
int num = int.Parse(Console.ReadLine());
int maxDivider = (int) Math.Sqrt(num);

while (prime && (divider <= maxDivider))
{
	if (num % divider == 0)
	{
		prime = false;
	}
	divider++;
}
Console.WriteLine( "Prime? " + prime);

// Enter a positive number: 4
// Prime? False

// Enter a positive number: 11
// Prime? True