Tuesday 2 February 2016

Multiply/Divide two numbers without asterisk (*) Slash (/) Operator in php

This tutorial is for beginners. For those who are having troubles solving their different problems and are starting the course of programming.

So we want to multiply to numbers without using the operators. The answer to this question is really easy and the logic is given below.

Assuming that we have two numbers and thwy ar ebeing taken in two variables



  1. First Number
  2. Second Number

Multiplication Logic


$first_num = $_POST['first_num'];
$sec_num = $_POST['sec_num'];
$result = 0;
for ($i = 0; $i <=; $first_num; $i++)
{
$result += $sec_num;

}
echo $result;


This will produce the output we want. 

Division Logic

$first_num = $_POST['first_num'];
$sec_num = $_POST['sec_num'];
$result = 0;
while($first_num>=$sec_num)
{
$first_num = $first_num-$sec_num;  //For Remainder
$result += 1;  //For Quotient
}
echo "the quotient is = ".$first_num;
echo '
';
echo "the remainer is = ".$result;


Hope this solves your  problem.

No comments:

Post a Comment