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:
<?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:
