Programming Course in C# ¡Free!

Function IsPrimeTarea

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not:

if (isPrime(127)) ...

Output



Solution


using System;
public class FunctionIsPrime
{
public static bool IsPrime(int n)
{
for (int i = 2; i < n; i++)
if (n %i == 0) 
return false;

return true;
}

public static void Main()
{
if (IsPrime(127))
Console.WriteLine("Prime");
else
Console.WriteLine("Not prime");
}
}