1) Write a script to accept two integers(Use html form having 2 textboxes). Write a PHP script to, a. Find mod of the two numbers. b. Find the power of first number raised to the second. c. Find the sum of first n numbers (considering first number as n) d. Find the factorial of second number. (Write separate function for each of the above operations.)
PHP CODE:
<?php
$a=$_GET['num1'];
$b=$_GET['num2'];
$ch=$_GET['op'];
switch($ch)
{
case 1 : mod($a,$b);
break;
case 2 : power($a,$b);
break;
case 3 : sumN($a);
break;
case 4 : fact($b);
break;
}
function mod($a,$b)
{
$c=$a%$b;
echo "Mod of first and second number : ";
echo $c;
}
function power($a,$b)
{
$ans=1;
$n1=$a;
while($n1>0)
{
$ans=$ans*$b;
$n1--;
}
echo "$b raised to power $a : $ans\n";
}
function sumN($a)
{
$n2=$a;
$n=$n2;
$sum=0;
while($n>=0)
{
$sum=$sum+$n;
$n--;
}
echo "Sum of $n2 numbers is $sum\n";
}
function fact($b)
{
$n2=$b;
$facto=1;
for($x=$n2;$x>=1;$x--)
{
$facto=$facto*$x;
}
echo "Factorial of $b is $facto\n";
}
?>
HTML CODE:
<html>
<body>
<form action="a1.php" method="GET">
First Number : <input type="number" name="num1"><br>
Second Number : <input type="number" name="num2"><br>
<p>Which Operation do you want to perform ? </p>
<input type="radio" name="op" value="1">
<label for="mod">Mod of the two numbers</label><br>
<input type="radio" name="op" value="2">
<label for="power">Power of first number raised to second number</label><br>
<input type="radio" name="op" value="3">
<label for="sum">Sum of first n numbers(considering first number as n)</label>
<br>
<input type="radio" name="op" value="4">
<label for="fact">Factorial of second number</label><br>
<input type="submit">
</form>
</body>
</html>
Comments
Post a Comment