Tuesday 9 January 2018

Control Statements Programs in PHP


To check whether an alphabet is vowel or consonant
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter Character:
<input type="text" name="chr" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$ch = $_POST['chr'];
if(isset($_POST['submit']))
 {
 if (($ch >= 'a' && $ch <= 'z') || ($ch >= 'A' &&$ch <= 'Z'))
 {
 If ($ch=='a' || $ch=='A' || $ch=='e' || $ch=='E' || $ch=='i' || $ch=='I' || $ch=='o' || $ch=='O' || $ch== 'u' || $ch=='U')
{
            echo " $ch  is a vowel</br> "; 

}
   else
   {
   echo " $ch  is not a vowel </br> "; 
    }
}   
}
?>

To check  a number is Odd or Even? 
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter Number:
<input type="text" name="num" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$number= $_POST['num'];
if(isset($_POST['submit']))
{
 if ($number%2 == 0)
 {
            echo " It is Even Number</br> "; 

  }
 
   else
   {
   echo " It is  Odd Number </br> "; 
    }
}   

?>

To check  whether a year is  leap year or Not
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter Year:
<input type="text" name="num" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$year= $_POST['num'];
if(isset($_POST['submit']))
{
 if ($year%4 == 0)
 {
            echo " It is Leap Year</br> "; 

  }
 
   else
   {
   echo " It is not Leap Year </br> "; 
    }
}   

?>

Calculate sum of digits of a number
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter Number:
<input type="text" name="num" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$number= $_POST['num'];
$sum = 0;
if(isset($_POST['submit']))
{
   $t = $number;
  while ($t != 0)
   {
      $remainder = $t % 10;
      $sum = $sum + $remainder;
      $t= $t / 10;
   }
  echo "Sum of digits is  $sum";
}
?>

Calculate Factorial Number
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter Number:
<input type="text" name="num" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$number= $_POST['num'];
$fact=1;
if(isset($_POST['submit']))
{
   while($number>0)
  {
      $fact=$fact*$number;
      $number--;
  }
  echo "factorial is $fact";
}
?>

Find square and cube of a given number
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter Number:
<input type="text" name="num" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>
result.php
<?php
$number= $_POST['num'];
if(isset($_POST['submit']))
{
  $square=$number*$number;
  $cube=$number*$number*$number;
  echo "Square of the number is $square</br>";
  echo "Cube of the number  is $cube";
}

?>

Print "Hello World" without using semi colon

<?php
  if(echo("Hello World"))
   {
 }

?>


To Check Whether a Number is Prime or Not
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter Number:
<input type="text" name="num" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$number= $_POST['num'];
$pm = 0;
if(isset($_POST['submit']))
{
for($i=1; $i<=$number; $i++)
   {
      if($number%$i==0)
      {
         $pm++;
      }
   }
   if($pm==2)
   {
      echo "$number is a prime number</br>";
   }
   else
   {
      echo "$number is not a prime number</br>";
   }
}
?>

Swapping of two numbers
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter  1st Number:
<input type="number" name="num1" /><br>
Enter  2nd Number:
<input type="number" name="num2" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$x= $_POST['num1'];
$y= $_POST['num2'];
if(isset($_POST['submit']))
{
echo "Value of x before swapping:$x</br>";
echo "Value of y before swapping:$y</br>";
$temp=$x;
$x=$y;
$y=$temp;
echo "Value of x after swapping: $x</br>";
echo "Value of y after swapping: $y</br>";
}
?>

Swapping of two numbers without using third variable
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter  1st Number:
<input type="number" name="num1" /><br>
Enter  2nd Number:
<input type="number" name="num2" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$x= $_POST['num1'];
$y= $_POST['num2'];
if(isset($_POST['submit']))
{
echo "Value of x before swapping:$x</br>";
echo "Value of y before swapping:$y</br>";
$x=$x+$y;
$y=$x-$y;
$x=$x-$y;
echo "Value of x after swapping: $x</br>";
echo "Value of y after swapping: $y</br>";
}
?>

Calculate Area of triangle
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter  base:
<input type="number" name="num1" /><br>
Enter  height:
<input type="number" name="num2" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$b= $_POST['num1'];
$h= $_POST['num2'];
if(isset($_POST['submit']))
{
$result=0.5*$b*$h;

echo "Area of triangle is:$result</br>";

}
?>

Find the Largest Number Among Three Numbers
Demo.html
<html>
<body>
<form method="post" action="result.php">
Enter  Number1:
<input type="number" name="num1" /><br>
Enter  Number2:
<input type="number" name="num2" /><br>
Enter  Number3:
<input type="number" name="num3" /><br>
<input  type="submit" name="submit" value="Check">
</form>
</body>
</html>

result.php
<?php
$a= $_POST['num1'];
$b= $_POST['num2'];
$c= $_POST['num3'];
if(isset($_POST['submit']))
{
if($a>=$b && $a>=$c)
{
         echo "The largest number is $a</br>";
}
      else if($b>=$a && $b>=$c)
              {
         echo "The largest number is $b</br>";
              }
      else
              {
       echo "The largest number is $c</br>";
              }

}
?>


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...