Prime number program fails with certain outlier numbers

  c++, math, primes

Hey guys any help would be appreciated:

Doing a Code wars problem
to find a prime number given an integer value. It seems my program is working properly but for some reason, it fails the prime number test. There seem to be some outlier numbers that cause it to fail but I can’t find it within my code do any of you see my fluke?

#include<iostream>

bool isPrime(double num) {

bool flag = false;
double x;
double y;
x = num;
y = num;

if (num <= 1 )
{
    flag = false;
}
else
{
    double i = 2;
    while(i < 9)
    {

        if (i != num)
        {
            x = num / i;
            y = num / i;
        }

        if (trunc(x) != y || num == 2)
        {
            flag = true;
            i++;

        }
        else
        {
            flag = false;
            i += 9;
        }
    }
}
  return flag;
}


int main()
{
int z = 0;

while (z != 100000000)
{
    if (isPrime(z) == false)
    {
        std::cout << z << " is False" << std::endl;
    }
    else if (isPrime(z) == true)
    {
        std::cout << z << " is True" << std::endl;
    }
    z++;
}
return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT