PHP - check if number is prime

后端 未结 18 1787
野趣味
野趣味 2021-01-25 00:03

I\'m trying to create a function which checks whether the number is prime or not. BUT I want this function to echo to the user \'prime\' or \'NOT prime\' - and that\'s where my

18条回答
  •  灰色年华
    2021-01-25 00:39

    I made a similar one where a user types in a number and PHP checks if the number is prime or not.

    HTML

    See if your number is a prime number

    PHP

    if ($_GET) {
    
        $i = 2;
    
        $isPrime = true;
    
        while ($i < $_GET['number']) {
    
            if ($_GET['number'] % $i == 0){
    
                // Number is NOT prime
                $isPrime = false;
    
            }
    
            $i++;
        }
    
        if ($isPrime){
    
            echo '

    '.$i.' is a prime number!'; } else { echo '

    '.$i.' is NOT a prime number'; } }

    Hopefully this works for you.

提交回复
热议问题