Is this Number Prime


/ Published in: C#

Method to Check if an int is a Prime Number


Copy this code and paste it in your HTML
  1. static bool isPrime(int number)
  2. {
  3.  
  4. if (number == 1) return true;
  5. if (number == 2) return true;
  6. if (number == 3) return true;
  7. if (number == 4) return false;
  8.  
  9. for (int i = 2; i <= Math.Ceiling(Math.Sqrt(Convert.ToDouble(number))); i++)
  10. {
  11. if (number % i == 0)
  12. {
  13. return false;
  14. }
  15. }
  16.  
  17. return true;
  18.  
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.