Let’s write a simple PHP Program to find the Quotient & Remainder. Before writing the program, let’s see a small example of what is a Remainder and a Quotient in mathematics.
Here is an example:
45 ÷ 7 = 6 and 3
Dividend Divisor Quotient Remainder
In the above example, 45 is the Dividend, 7 is the Divisor, 6 is the Quotient and 3 is the Remainder.
So the remainder is the “left over” value after dividing one integer by another. Quotient is the quantity produced by the division of 2 integers.
In PHP we can achieve the same with / and %, for eg:
quotion = dividend / divisor -> will give the quotient
remainder = dividend % divisor -> will give the remainder
Let’s write a Simple PHP Script to find the Remainder & Quotient dynamically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php if(isset($_POST['submit'])){ $dividend = $_POST['dividend']; $divisor = $_POST['divisor']; //to find the quotient use / operator $quotient = $dividend / $divisor; //to find the remainder use % modules $remainder = $dividend % $divisor; } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>PHP to find remainder & quotient of a number</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Load Bootstrap library for better UI look & feel --> <link rel="stylesheet" type="text/css" media="screen" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" /> </head> <body> <div class="container"> <br /> <h2>PHP to find remainder & quotient of a number</h2> <br /> <div class="row"> <div class="col-lg-4"> <div class="input-group mb-4"> <form action="" method="post"> <input type="number" class="form-control" name="dividend" placeholder="Base" value="<?=(isset($dividend))? $dividend : '45'; ?>" > <div class="input-group-prepend"> <span class="input-group-text">/</span> </div> <input type="number" class="form-control" name="divisor" placeholder="Exponent" value="<?=(isset($divisor))? $divisor : '7'; ?>" > <button type="submit" name="submit" class="btn btn-primary">Submit</button> </form> </div> <div><strong>Result:</strong><br /> <!-- Print the result --> <?=isset($remainder) ? '<strong>Remainder: </strong>'. $remainder :'';?> <?=isset($quotient) ? '<br><strong>Quotient: </strong>'. round($quotient) :'';?> <div> </div> </div> </div> </body> </html> |
Just go through the above code to understand how it works.
You can see the demo of the above PHP script here: