Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

PHP code to find the sum of digits of a number using for loop

This program finds the sum of the digits of the given number using for loop.


Code


<html>
<head>
<title>SUM OF THE DIGITS</title>
</head>
<body>
<font size=5>SUM OF THE DIGITS</font>
<br/><br/>
<form action=sum-of-digits.php method="post">
ENTER NUMBER:<input type="text" name="t1">
<br><br>
<input type="submit" value="SUM">
<?php
if(isset($_POST["t1"]))
{
$n=$_POST["t1"];
$sum = 0;
for(;$n>0;$n=floor($n/10))
{
$b=$n%10;
$sum=$sum+$b;
}
echo "sum of digit is:$sum";
}
?>
</form>
</body>
</html>

Output



sum of digits of a number in php.
Sum of the digits of a number

PHP Code to find the reverse of a number

Description


This program finds the reverse of a number using do while loop.

code


<html>
<head>
<title>REVERSE OF A NUMBER</title>
</head>
<body>
<font size=7>REVERSE OF A NUMBER</font>
<form action=reverse.php method="post">
ENTER NUMBER:<input type="text" name="t1">
<br><br>
<input type="submit" value="REVERSE">
<?php
if(isset($_POST["t1"]))
{
$n=$_POST["t1"];
$rev=0;
do
{
$t=$n%10;
$rev=($rev*10)+$t;
$n=floor($n/10);
}
while($n>0);
echo 'Reverse is ';
echo $rev;
}
?>
</form>
</body>
</html>

Output



PHP Program to find the reverse of a number

PHP program to find factorial using while loop

Code


<html>
<head>
<title>FACTORIAL OF A NUMBER</title>
</head>
<body>
<font size=5>FACTORIAL OF A NUMBER</font>
<br/>
<br/>
<form action=factorial.php method="post">
ENTER A NUMBER:<input type="text" name="t1">
<br><br>
<input type="submit" value="FACTORIAL">
<?php
if(isset($_POST["t1"]))
{
$n=$_POST["t1"];
$fact=1;
echo "<br/>
<br/>";
echo "Factorial of $n is: ";

while($n>0)
{
$fact=$fact*$n;
$n--;
}
echo "$fact";
}
?>
</form>
</body>
</html>

Output



PHP program to find factorial using while loop
PHP program to find the factorial of a number

PHP code to find the largest of 3 numbers

Explanation of the code


This program finds the largest of three numbers. The three text boxes named t1, t2 and t3 is used to read the numbers. The php code will execute only if the submit button is clicked, this is achieved by using the statement if(isset($_POST["submit"])). When the submit button is clicked, the values entered in the text boxes will be stored in the variables n1, n2 and n3 respectively. Then the program will find the largest of these three numbers using if else condition check.

PHP code


<html>
<head>
<title>GREATEST OF THREE NUMBERS</title>
</head>
<body>
<font size=5>LARGEST OF THREE NUMBERS</font>
<br/>
<br/>
<form action="largest.php" method="post">
ENTER FIRST NUMBER:<input type="text" name="t1">
<br><br>
ENTER SECOND NUMBER:<input type="text" name="t2">
<br><br>
ENTER THIRD NUMBER:<input type="text" name="t3">
<br><br>
<input type="submit" value="CHECK" name="submit">
<?php
if(isset($_POST["submit"]))
{
$n1=$_POST["t1"];
$n2=$_POST["t2"];
$n3=$_POST["t3"];
if($n1>$n2)
{
if($n1>$n3)
echo "$n1 is greater";
else
echo "$n3 is greater";
}
else if($n2>$n3)
{
if($n2>$n1)
{
echo "$n2 is greater";
}
else
{
echo "$n3 is greater";
}}}
?>
</form>
</body>
</html>

Output




Largest of three numbers in PHP
Largest of three numbers in PHP