write a function that determine whether a number is prime.

Ancient Dragon commented: Create multiple threads for the same question, and no attempt to do it yourself. -5

Recommended Answers

All 3 Replies

Do it yourself.

Why don't you try searching the forums? The whole point of keeping the code in previous threads is so people will be able to learn from them in the future. I've seen this question asked many times in the past few weeks. Or you can actually try doing it yourself (that would be the prefered method) ;)

Hi,

simple solution would be this boolean method...

returns true if it is prime, false otherwise...

#include <cmath>

bool IsPrime(int num)
{
      if(num == 0)
         return true;

     num = abs(num);

     for(int i = 2; i <= sqrt(num); i++)
     if(num % i == 0)
        return false;
  
     return true;   
}

ps...google

commented: Please do not give away homework answers. +0
commented: helpful tbh +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.