Jump to content

Need help in Checking divisibility of Numbers in Php


Marie

Recommended Posts

I have a form on one page that submits to another page. There, it checks if the number is divisible by another number. Please check the code, What is missing or wrong?

 

        <?php
        
            if(isset($_POST['submit']))
            {
                if(is_numeric($_POST['num1']) && is_numeric($_POST['num2']))
                {

                    if($_POST['operation'] == 'divided by')
                    {

                        if ($num1 % $num2 == 0)
                        {
                            echo $_POST['num1'] . ' Is Divisible By ' . $_POST['num2'];
                        }
                        else
                        {
                            echo $_POST['num1'] . ' Is Not Divisible By ' . $_POST['num2'];
                        }
                        
                        echo "<h1>{$_POST['num1']} {$_POST['operation']} {$_POST['num2']} equals {$answer}</h1>";
                
                }
                else {
                    
                    echo 'Numeric values are required';
                }
                }
            }
    ?>

Link to comment
Share on other sites

  • 2 months later...
On 9/6/2022 at 8:33 AM, Marie said:

I have a form on one page that submits to another page. There, it checks if the number is divisible by another number. Please check the code, What is missing or wrong?

 

        <?php
        
            if(isset($_POST['submit']))
            {
                if(is_numeric($_POST['num1']) && is_numeric($_POST['num2']))
                {

                    if($_POST['operation'] == 'divided by')
                    {

                        if ($num1 % $num2 == 0)
                        {
                            echo $_POST['num1'] . ' Is Divisible By ' . $_POST['num2'];
                        }
                        else
                        {
                            echo $_POST['num1'] . ' Is Not Divisible By ' . $_POST['num2'];
                        }
                        
                        echo "<h1>{$_POST['num1']} {$_POST['operation']} {$_POST['num2']} equals {$answer}</h1>";
                
                }
                else {
                    
                    echo 'Numeric values are required';
                }
                }
            }
    ?>

To check if a number is not a divisible of another number, we can use the modulo operator % but the remainder of first number by second number is not equal to 0.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.