Tuesday 13 May 2014

To Find The Factorial Of A Number Using PHP

A factorial of a number n is the product of all the numbers between n and 1.
The easiest way to calculate it is with a for( )
loop which one that starts at n and counts down to 1. Each time the loop runs,
the previously calculated product is multiplied by
the current value of the loop counter and the result is the factorial of the number n.


To find the factorial number, you can use a loop to count down and multiply the number by all the numbers between itself and 1 such as:

<?php
$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--) {
  $factorial = $factorial * $x;
  echo $x."</br>";
}
echo "Factorial of $num is $factorial";
?>

Output will be :

4
3
2
1
Factorial of 4 is 24

To find the factorial of a number using form in php

index.php

<?php
/* Function to get Factorial of a Number */
function getFactorial($num)
{
    $fact = 1;
    for($i = 1; $i <= $num ;$i++)
        $fact = $fact * $i;
    return $fact;
}
?>
<!doctype html>
<html>
<head>
<title>Factorial Program using PHP</title>
</head>
<body>
<form action = "" method="post">
Enter the number whose factorial requires <Br />
<input type="number" name="number" id="number" maxlength="4" autofocus required/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit']) and $_POST['submit'] == "Submit")
{
    if(isset($_POST['number']) and is_numeric($_POST['number']))
    {
        echo 'Factorial Of Number: <strong>'.getFactorial($_POST['number']).'</strong>';
    }
}

?>
</body>
</html>


 Output will be :

if you enter 5 and click to submit then :

Enter the number whose factorial requires 
 
Factorial Of Number: 120


 

Saturday 10 May 2014

To Find Second Largest Value From An Array

Here is the example to find the second largest value from an array.

array.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array</title>
</head>

<body>
<h1>To Find Second Largest Value From An Array</h1>
<?php 
function second_largest($arr)
{
sort($arr,SORT_NUMERIC);
return($arr[count($arr)-2]);
}
echo "Second Largest Value From Array is :".second_largest(array(5,4,3,1,8,9,10,2));
?>
</body>
</html>

OUTPUT :
To Find Second Largest Value From An Array
Second Largest Value From Array is :9

To Find The Largest And Smallest Values From An Array

max() function is a properties of an array and  is used to find the largest value from an array.

Example :
max.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array</title>
</head>
<body>
<h1>To Find Largest Value From An Array</h1>
<?php 
$array=array(2,3,5,4,8,10,150,1);
$a=max($array);
echo "Largest Value From An Array is :".$a."</br>";
?>
</body>
</html>

Note : To Find The Smallest Value From An Array we use min() function. 
min() function is a properties of an array and  is used to find the smallest value from an array.

Example :
<?php 
$array=array(2,3,5,4,8,10,150,1);
$a=min($array);
echo "Smallest Value From An Array is :".$a."</br>";
?>

Friday 2 May 2014

Add A Calendar Date Picker To A Form Using jQuery

Here is an example, try it.

In this code when you click in text box then calender is automatically generated.

 index.html


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
 <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
   <script>
  $(document).ready(function() {
    $("#datepicker").datepicker();
      });</script>
</head>

<body>
 Date: <input type="text" id="datepicker">
       
</body>
</html>


Examples of PHP Program Using Array


Array can be intialized in three ways in PHP.

1st Way :   $col=array("blue","red","green","white","pink");

2nd Way :   $col[ ]="blue";
                    $col[ ]="red";
                   $col[ ]="green";
                   $col[ ]="white";
                   $col[ ]="pink";

3rd Way :   $col[0]="blue";
                   $col[1]="red";
                   $col[2]="green";
                   $col[3]="white";
                   $col[4]="pink";

Here you can find the Example please click below
Example

Thursday 1 May 2014

PHP Program To Find Reverse Of A Number Or String

<?php
$int = "Ritu Raj";// here you can use any number or string
$reverse = strrev($int); // for reversing the string
echo "String: " . $int . "<br>";
echo "Reverse String : " . $reverse ;
?>

OutPut:

String: Ritu Raj
Reverse String : jaR utiR

PHP Program To Find A Anagram

An anagram is a rearrangement of the letters of one word or phrase to form another word or phrase.
In other words :"Any word or phrase that exactly reproduces the letters in another order is an anagram."
For example, "orchestra" is a transposition of "carthorse",
                      "dog" is a transposition of "god",
                      "army" is a transposition of "mary".
Note : The original word or phrase is known as the subject of the anagram.

A simple PHP Program for Anagram is given below

<?php
if(empty($word)) $word="dog";
for($i=0;$i<strlen($word);$i++) {
  $letters[$i]=substr($word,$i,1);
}
shuffle($letters);
$anagram=implode($letters);
echo "One anagram of $word is $anagram.";
?>

Output :

One anagram of dog is god
One anagram of dog is gdo,
One anagram of dog is dgo
and more....

When you execute the program then subjected word will change after refresh the page and that word or pharse will be anagram of subjected word or phrase.

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...