Skip to content
Exercise · Conditions and loops · lesson 13 of 13

Exercise: is it prime

Write a method public static bool IsPrime(int n) that returns true if n is a prime number and false otherwise. A prime number is greater than 1 and is divisible only by 1 and itself.

The method header is already in the stub: keep it as it is and write your code between the curly braces. Store the answer in the variable prime: the last line, return prime;, hands its value back to the code that called the method.

  • IsPrime(2) → true
  • IsPrime(97) → true
  • IsPrime(91) → false

Constraints: n is from 0 to 10000; 0 and 1 are not prime.

The checks the site runs are on the tests tab: you can read it but not change it.

Do not add extra using directives unless the task says so explicitly.

Solution.cs — csharp
csharp · 5 lines
Ctrl+Enter run · Esc leave editor