0% found this document useful (0 votes)
99 views

Factorial Program

The document contains code for a PHP program that performs various operations on arrays. It includes HTML code to accept user input separated by commas and submit it to a PHP file. The PHP file uses explode() to convert the input into an array, then performs sorting, reverse sorting, removing duplicates, counting elements, and summing elements on the array. The output displays the results of each operation.

Uploaded by

ramshoban
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Factorial Program

The document contains code for a PHP program that performs various operations on arrays. It includes HTML code to accept user input separated by commas and submit it to a PHP file. The PHP file uses explode() to convert the input into an array, then performs sorting, reverse sorting, removing duplicates, counting elements, and summing elements on the array. The output displays the results of each operation.

Uploaded by

ramshoban
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

FACTORIAL PROGRAM <html> <title> <h2>find factorial</h2> </title> <form action="findfact.

php" method="POST"> enter a number:<input type="text" name="num"><br> <input type="submit"name="findfact" value="factorialvalue"> </form> </html>

<html> <body> <?php $num=$_POST["num"]; if($num==" ") echo"please enter a number"; else if($num<0) echo"please enter a positive number"; else echo"factorial of $num=".fact($num); function fact($num) { $result=1; for($i=1;$i<=$num;$i++) $result*=$i; return$result; } ?> </body> </html>

OUTPUT:

ARTHIMETIC OPERATION HTML: <html> <body> <h2>Mathematical Function<h2><br> <form action="operation.php" method="POST"> Enter a First Number:<input type="text" name="num1"><br> Enter a Second Number:<input type="text" name="num2"><br> <select name="op"> <option value="Add">Addition</option> <option value="sub">Subtraction</option> <option value="Multiply">Multiplication</option> <option value="Divide">Division</option> <option value="Module">Modularation</option> </select> <input type="submit" name="loop"value="calculate"> </form> </body> </html> PHP: <html> <body> <?php $op=$_POST["op"]; $num1=$_POST["num1"]; $num2=$_POST["num2"]; switch($op) { case"Add": echo"The Sum of $num1 and $num2=".Add($num1,$num2); break; case"Sub": echo"The Difference of $num1 and $num2=".Sub($num1,$num2); break; case"Multiply": echo"The Product of $num1 and $num2=".Multiply($num1,$num2); break; case"Divide": echo"The Quotient of $num1 and $num2=".Divide($num1,$num2); break; case"Module": echo"The Reminder of $num1 and $num2=".Module($num1,$num2);

break; } function Add($num1,$num2) { return$num1+$num2; } function Sub($num1,$num2) { return$num1-$num2; } function Multiply($num1,$num2) { return$num1*$num2; } function Divide($num1,$num2) { return$num1/$num2; if($num2!=0) return $num1/$num2; else echo"Invalid second number"; } function Module($num1,$num2) { return$num1%$num2; } ?> </body> </html>

OUTPUT:

ASSOCIATIVE ARRAY: <html> <head><title>table</title></head> <body><h1>countries and capital</h1> <?php $country_cap=array("India"=>"new delhi","washington"=>"dc","srilanka"=>"colombu","china"=>"beijing"); echo"<table border=\"1\">"; echo"<th>country</th><th>capital</th>"; foreach($country_cap as $country=>$capital) { echo"<tr><td>".$country."</td><td>".$capital."</td></tr>"; } echo"</table>"; ?> </body> </html>

OUTPUT:

STRING FUNCTION: HTML: <html> <body> <h2>String Function</h2><br> <form action="process.php" method="POST"> Enter a String of text:<input type="text" name="str"><br> <input type="Checkbox" name="trim">Trim<br> <input type="Checkbox" name="wordcnt">Word Count<br> <input type="Checkbox" name="strlen">String Length<br> <input type="Checkbox" name="strrev">String Reverse<br> <input type="Checkbox" name="strtoupper">String in lower<br> <input type="Checkbox" name="strtolower">String in upper<br> <input type="submit" name="text" value="submit"> </form> </body> </html> PHP: <html> <body> <?php $txt=$_POST["str"]; echo"<table border=\"1\">"; echo"<th>Operation</th><th>Output</th>"; if(isset($_POST["trim"])) echo"<tr><td>Trim</td><td>".trim($txt)."</td><tr>"; if(isset($_POST["wordcnt"])) echo"<tr><td>Word Count</td><td>".str_word_count($txt)."</td></tr>"; if(isset($_POST["strlen"])) echo"<tr><td>String Length</td><td>".strlen($txt)."</td></tr>"; if(isset($_POST["strrev"])) echo"<tr><td>String Reverse</td><td>".strrev($txt)."</td></tr>"; if(isset($_POST["strtoupper"])) echo"<tr><td>Stringtoupper</td><td>".strtoupper($txt)."</td></tr>"; if(isset($_POST["strtolower"])) echo"<tr><td>Stringtolower</td><td>".strtolower($txt)."</td></tr>"; echo"</table>"; ?> </body> </html> :

OUTPUT

ARRAY PROGRAM:

HTML: <html> <body> <h2>Input for performing the following operation</h2> <ul> <li>Sort</li> <li>Reverse Sort</li> <li>Unique</li> <li>Count</li> <li>Sum</li> </ul> <form action="arrayprocess.php" method="POST"> Enter a string of text or numbers seperated by,:<br> <input type="text" name="input"> <input type="submit" value="Do operation"> </form> </body> </html>

PHP: <html> <body> <?php $str=$_POST["input"]; $arrval=explode(".",$str); function display(&$arrval) { for($i=0;$i<sizeof($arrval);$i++) { echo $arrval[$i]."<br>"; } } display($arrval); echo"The array after Sorting<br>\n"; sort($arrval); display($arrval); echo"The array after Reverse Sorting<br>\n"; rsort($arrval); display($arrval);

echo"The array after removed of duplicate elements array are<br>\n"; $uniq=array_unique($arrval); display($uniq); echo"No fo elements in the array:".Count($arrval); echo"The Sum of the elements in the array:".array_Sum($arrval); ?> </body> </html>

OUTPUT:

MESSAGE PASSING BETWEEN PAGES: HTML: <html> <body> <a href="passing.php?name=arun & city=trichy"> click the link to get a welcome message</a> </body> </html> PHP: <html> <body> <?php $name=$_REQUEST["name"]; $city=$_REQUEST["city"]; echo"Hai $name welcome to $city"; ?> </body> </html>

OUTPUT:

DATABASE PROGRAM CREATE DATABASE: <?php $con=mysql_connect("localhost","root",""); if($con) echo"connected sucessfully"; else echo"unable to connect"; $sql="CREATE DATABASE IF NOT EXISTS studentdb"; mysql_query($sql) or die(mysql_error()); mysql_close($con); ?> CREATE TABLE: <html> <body> <?php $con=mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("studentdb",$con) or die(mysql_error()); $createtable="CREATE TABLE divya(stud_id int NOT NULL auto_increment, stud_name varchar(50) NOT NULL,stud_dob date NOT NULL, stud_course varchar(20) NOT NULL,stud_city varchar(20) NOT NULL,PRIMARY KEY(stud_id))"; mysql_query($createtable) or die(mysql_error()); mysql_close($con); ?> </body> </html>

POPULATED.PHP <html> <body> <?php

$con=mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("studentdb",$con) or die(mysql_error()); $val="INSERT INTO divya(stud_name,stud_dob,stud_course,stud_city)". "VALUES('divya','1990-11-07','BCA','Srirangam'),". "('vaiju','1990-12-04','BCA','Townstation'),". "('saranya','1991-05-06','BCA','Srirangam'),". "('nive','1991-06-26','BCA','namakkal'),". "('dhivya','1991-11-13','BCA','Srirangam')"; mysql_query($val) or die(mysql_error()); mysql_close($con); ?> </body> </html> DISPLAY.PHP <html> <body> <?php $con=mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("studentdb",$con) or die(mysql_error()); $qry="SELECT*FROM divya"; $res=mysql_query($qry) or die(mysql_error()); echo"<table border=\"1\">"; echo"<tr><th>Student Id</th><th>Student Name</th><th>Date of Birth</th><th>Course</th><th>City</th></tr>"; while($row=mysql_fetch_assoc($res)) { echo"<tr>"; foreach($row as $val) echo"<td>$val</td>"; echo"<tr>"; } echo"</table>"; mysql_close($con); ?> </body> </html>

OUTPUT:

DATE AND TIME: <?php echo"Today's date is :".date("d-M-Y")."<br>\n"; echo"TOday's time is:".date ("h-i-A")."<br>\n"; $tom=strtotime("tommorrow"): echo"Tommorrow is".date("d-M-Y",$tom)."<br>\n"; $yes=strtotime("Yesterday"); echo"Yesterday is".date("d-M-Y"<$yes).<br>\n"; $nmom=Strtotine("Next month"); echo"Next Month id".date("d-M-Y"$nmon)."<br>\n"; $nyear=strtotime("Next Year"): echo"Next Year is".date("d-M-Y",$nyear)."<br>\n"; $lyear=strtotime("Last Year"); echo"LastYear is".date("d-M-Y",$nyear)."<br>\n"; ?>

OUTPUT:

FILE SYSTEMS: HTML: <html> <head> <title>file system functions</title> </head> <body> <form action="fieldprocess.php" method="POST"> Enter a Full path name:<input type="text" name="path"><br> <input type="submit" value="file or dir"> </form> </body> </html> PHP: <?php $pathname=$_POST["path"]; if(!file_exists($pathname)) { echo"$pathname does not exists<br>\n"; exit; } if(is_file($pathname)) { echo"$pathname is a file<br>\n"; $pinfo=pathinfo($pathname); echo"The directory name is".$pinfo['dirname']."<br>"; echo"The filename is".$pinfo['base']."<br>"; echo"The file extension is".$pinfo['extension']."<br>"; } if(is_dir($pathname)) { echo"$pathname is a directory"; echo"The file in the directory are:<br>"; $dh=opendir($pathname); while($filename=readdir($dh)) { echo"$filename<br>\n"; } } ?> </body> </html>

OUTPUT:

COOKIES PROGRAM: <html> <body> <form action="cookieset.php" method="POST"><br> Nmae:<input type="text" name="myname"> State:<input type="text" name="state"> <input type="submit" value="set cookie"> </form> </body> </html> COOKIESET.PHP: <html> <body> <?php setcookie("namecookie",$_POST['myname']); setcookie("statecookie",$_POST['state']); echo"<a href=\"getcookie.php\">click here to view the cookie values</a>"; ?> </body> </html> GETCOOKIE.PHP: <html> <body> <?php $name=$_COOKIE['namecookie']; $state=$_COOKIE['statecookie']; echo"your name is $name<br>"; echo"your state is $state<br>"; ?> </body> </html>

OUTPUT:

OUTPUT:

You might also like