Problem: A Prime number program

Discussion in 'Scripts' started by goldenwaters, Feb 20, 2012.

to remove all ads.
  1. goldenwaters

    goldenwaters Member

    Messages:
    114
    Hello friends,
    please dont feel disappointed by my question. Im not too perfect in php.
    Im battling at writing a prime number program. I did it to this point: if the textbox is empty, it tells the user to input a number. When a number less than 2 is entered, it tells the user to input a higher value. Now my problem is, when i input a greater than 2 number like 4, it still tells me its a prime number. Please help me.

    The source code is:

    <?php

    //collects the number entered by user.

    $num = $_POST['num'];

    if(empty($num)){
    die("ERROR: Please enter a number.");
    }

    if($num<2){
    echo "This is no prime number.";
    }
    elseif($num %= 1 && ...){ // i have problems here completing this line of code.
    echo "This is a prime number";
    }
    else{
    echo "This is NOT a prime number";
    }

    ?>

    ------------------
    Thanks,
    Golden Waters.
     
  2. conor

    conor New Member

    Messages:
    348
    something like this should work:

    Code:
            if( $num % 2 == 0 )
                    echo $num . ' is a prime number';
            else
                    echo $num . ' is not a prime nuber';
    
     
  3. goldenwaters

    goldenwaters Member

    Messages:
    114
    Thanks man, it worked!

    ----------
    Thanks,
    Golden Waters
     
  4. Phreaddee

    Phreaddee Super Moderator Staff Member

    Messages:
    1,978
    except for 9, 15, 21 etc.
     

Share This Page