Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Name:-Sachin Malviya

Roll Number:-41

ASSIGNMENT1
Q1)Write a program to convert temperature in degree celcius to
degree Fahrenheit and vice versa.

⮚ Code:-
<?php
$c="";
$f="";
$result="";

if (isset($_POST['ferenheit']))
{
$c=@($_POST['temp']);
$c=9/5*$c+32;
$result=$c."in ferenheit";
}
if (isset($_POST['celcius']))
{
$f=@($_POST['temp']);
$f=5/9*($f-32);
$result=$f."in celcius";
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="TempratureConverter.php"method="post">
<table>
<tr>
<td>Enter Temperature</td>
<td><input type="text" name="temp"></td>
</tr>
<tr>
<td><input type="submit" name="ferenheit"
value="ferenheit"></td>
<td><input type="submit" name="celcius"
value="celcius"></td>
</tr>
<tr>
<td>
<?php
echo $result;
?>
</td>
</tr>
</table>
</form>
</body>
</html>
⮚ Output:-
ASSGNMENT2
Q2)Write a program to take input from a user in seconds,
convert it into a number of weeks, days, hours, minutes,
seconds.

⮚ Code:-
<?php
$sec="";
$min="";
$hrs="";
$days="";
$weeks="";
if(isset($_POST['Check']))
{
$sec=@($_POST['second']);

$min=(int)($sec/60);
$sec=(int)($sec%60);

$hrs=(int)($min/60);
$min=(int)($min%60);

$days=(int)($hrs/60);
$hrs=(int)($hrs%60);

$weeks=(int)($days/60);
$days=(int)($days%60);
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="Time.php" method="post">
<table>
<tr>
<td>
Enter Seconds
</td>
<td>
<input type="text" name="second">
</td>
</tr>
<tr>
<td>
<input type="submit" value="Check"
name="Check">
</td>
</tr>
<tr>
<td>
<?php
echo
$weeks."Weeks,".$days."Days,".$hrs."Hours,".$sec."Secon
ds";
?>
</td>
</tr>
</table>
</form>
</body>
</html>

⮚ Output:-
ASSIGNMENT3
Q3)Write a program to calculate Area of square, Rectangle
and Trapezium.

⮚ Code:-
<?php
$radius="";
$length="";
$breadth="";
$length1="";
$length2="";
$height="";
$result1="";
$result2="";
$result3="";

if(isset($_POST['square']))
{
$radius=@($_POST['radius']);
$result1=(float)$radius*$radius;
}
if(isset($_POST['rectangle']))
{
$length=@($_POST['length']);
$breadth=@($_POST['breadth']);
$result2=(float)$length*$breadth;
}
if(isset($_POST['Trapezium']))
{
$length1=@($_POST['length1']);
$length2=@($_POST['length2']);
$height=@($_POST['height']);
$result3=(float)($length1+$length2)*$height/2;
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="Areas.php" method="post">
<table>
<tr>
<td>
Enter Radius
</td>
<td>
<input type="text" name="radius">
</td>

</tr>
<tr>
<td>
<input type="submit" value="Area of
Square" name="square">
</td>
</tr>
<tr>
<td>
Enter Length
</td>
<td>
<input type="text" name="length">
Enter breadth
<input type="text" name="breadth">

</td>
</tr>
<tr>
<td>
<input type="submit" value="Area of
Rectangle" name="rectangle">
</td>
</tr>
<tr>
<td>
Enter length of two parralel sides
</td>
<td>
Length1<input type="text" name="length1">
Length2<input type="text" name="length2">
Enter Height<input type="text"
name="height">
</td>
</tr>
<tr>
<td>
<input type="submit" value="Area of
Trapezium" name="Trapezium">
</td>
</tr>
</table>
<table>
<tr>
<td>Area of Square</td>
<td>
<?php
echo $result1;
?>
</td>
</tr>
<tr>
<td>Area of Rectangle</td>
<td>
<?php
echo $result2;
?>
</td>
</tr>
<tr>
<td>Area of trapezium</td>
<td>
<?php
echo $result3;
?>
</td>
</tr>
</table>
</form>
</body>
</html>

⮚ Output:-
ASSIGNMENT4
Q4)Write a program to print following pattern.
1
12
123
1234
⮚ Code:-
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="patternprogram.php" method="get">
<table>
<tr>
<td>
Enter number
</td>
<td><input type="text" name="num1"></td>
</tr>
<tr>
<td>
<input type="submit" value="Print"
name="print">
</td>
</tr>
<tr>
<td>

</td>
</tr>
</table>
</form>
</body>
</html>
<?php
$i="";
$j="";
$num="";
$result="";
if(isset($_GET['print']))
{
$num=@(isset($_GET['num1']));
for($i=1;$i<=$num;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j." ";
}
echo "<br/>";
}
}
?>

⮚ Outputs:-

ASSIGNMENT5
Q5)Write a program to print maximum between three
numbers.
⮚ Code:-
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="MaximumBetweenThree.php"
method="post">
<table>
<tr>
<td>Enter First Number</td>
<td><input type="text" name="num1"></td>
</tr>
<tr>
<td>Enter Second Number</td>
<td><input type="text" name="num2"></td>
</tr>
<tr>
<td>Enter Third Number</td>
<td><input type="text" name="num3"></td>
</tr>
<tr>
<td><input type="submit" name="check"
value="Check"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['check']))
{
$num1=@($_POST['num1']);
$num2=@($_POST['num2']);
$num3=@($_POST['num3']);
if($num1>$num2 && $num1>$num3)
{
echo $num1."is greatest";
}
else
{
if($num2>$num1 && $num2>$num3)
{
echo $num2."Is greatest";
}
else
{
echo $num3."is greatest";
}
}

}
?>
⮚ Outputs:-

ASSIGNMENT6
Q6)Write a program to print sum of first n natural number
without using direct formula.

⮚ Code:-
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="Question6.php" method="post">
<table>
<tr>
<td>
Enter a number
</td>
<td>
<input type="text" name="num">
</td>
</tr>
<tr>
<td>
<input type="submit" value="Check"
name="check">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
$i="";
$sum=0;
if(isset($_POST['check']))
{
$num=@($_POST['num']);
}
else
{
$num=5;
}
for ($r=1;$r<=$num;$r++)
{
for ($c=1;$c<=$r;$c++)

echo $c;
echo "<br>";
}
?>

⮚ Output:-
ASSIGNMENT7
Q7)Write a program to demonstrate the working of echo
and print method in php.

⮚ Code:-

<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
echo "ECHO Command\n";
echo '<br>';
echo "1) Uses any type of data as an argument but
converts it into string";
echo '<br>';
$a=32;
echo '<br>';
$b=12.3;
echo '<br>';
$c="Hello";
echo '<br>';
echo $a . " " . $b . " " . $c;
echo '<br>';
echo 'In above output it does not appends new line
character for different echo commands hence we have to
use special characters <br> or \n';
echo '<br>';
echo "\n2)Displays output of next variables: $a $b
$c\n";
echo '<br>';
echo '3)Does not Display output of next variables:
$a $b $c';
echo '<br>';
echo "\n4)May take multiple arguments but
seldomly used : ";
echo '<br>';
echo "\n",$a," ",$b,"\n\n";
echo '<br>';
print("Print:");
echo '<br>';
print("\n1)It is refered as a function in PHP ");
echo '<br>';
print("\n2)Like echo it also takes string argument
but only one");
echo '<br>';
print($a. " " . $b . " ".$c."\n");
echo '<br>';
print("3)Same as echo single quoted string does not
works for special characters or escape sequences:". '\n $a
$b $c ' . " But in double quoted it works" . "\n $a $b $c " );
echo '<br>';
print "\n4) We can skip () for print";
echo '<br>';
$r=print("\n5)It always returns value integer value 1
: Returned value = ");

print($r);
?>
</body>
</html>

⮚ Output:-

ASSIGNMENT8
Q8)Write a program to display all global variables currently active in memory.

⮚ Code:-
<?php
$x=54;
$nm="A B Shinde";
$perr=54.345;
foreach($GLOBALS as $keyy => $valuess)
{
echo "\n$keyy => ";
print_r($valuess);
}
?>

⮚ Outputs:-
C:\xampp\htdocs\PhpProject1\phpassingment>php globalvariable.php

_GET => Array


(
)

_POST => Array


(
)

_COOKIE => Array


(
)

_FILES => Array


(
)

argv => Array


(
[0] => globalvariable.php
)

argc => 1
_SERVER => Array
(
[ALLUSERSPROFILE] => C:\ProgramData
[APPDATA] => C:\Users\DELL\AppData\Roaming
[CLASSPATH] => C:\Program Files\Apache Software Foundation\Tomcat
9.0\lib\servlet-api.jar;C:\Program Files\Java\jdk1.8.0_281\bin;C:\Program
Files\Java\jre1.8.0_281\bin;
[CommonProgramFiles] => C:\Program Files (x86)\Common Files
[CommonProgramFiles(x86)] => C:\Program Files (x86)\Common Files
[CommonProgramW6432] => C:\Program Files\Common Files
[COMPUTERNAME] => PBELVALKAR
[ComSpec] => C:\WINDOWS\system32\cmd.exe
[DriverData] => C:\Windows\System32\Drivers\DriverData
[FPS_BROWSER_APP_PROFILE_STRING] => Internet Explorer
[FPS_BROWSER_USER_PROFILE_STRING] => Default
[HOMEDRIVE] => C:
[HOMEPATH] => \Users\DELL
[IntelliJ_IDEA_Community_Edition] => C:\Program Files\JetBrains\IntelliJ
IDEA Community Edition 2020.3.1\bin;
[JAVA_HOME] => C:\Program Files\Java\jdk-11.0.8
[LOCALAPPDATA] => C:\Users\DELL\AppData\Local
[LOGONSERVER] => \\PBELVALKAR
[NUMBER_OF_PROCESSORS] => 4
[OneDrive] => C:\Users\DELL\OneDrive
[OS] => Windows_NT
[Path] => C:\Program Files\Common
Files\Oracle\Java\javapath;C:\Program Files (x86)\Common
Files\Oracle\Java\javapath;C:\Windows\system32;C:\Program
Files\Java\jdk-11.0.8\bin;C:\Program
Files\dotnet\;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\WI
NDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WIND
OWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\Open
SSH\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files
(x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files
(x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program
Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files
(x86)\Brackets\command;C:\Program Files\Apache Software
Foundation\Tomcat 9.0\lib\servlet-api.jar;C:\Program Files (x86)\Microsoft
SQL Server\90\Tools\binn\;C:\xampp\php;C:\Program
Files\Java\jdk-16.0.1\bin;C:\Program Files\nodejs\;C:\Program
Files\MySQL\MySQL Shell
8.0\bin\;C:\Users\DELL\AppData\Local\Programs\Python\Python38\Script
s\;C:\Users\DELL\AppData\Local\Programs\Python\Python38\;C:\Users\DE
LL\.dotnet\tools;C:\Users\DELL\AppData\Local\Programs\Microsoft VS
Code\bin;C:\Program Files\JetBrains\PyCharm Community Edition
2020.2.3\bin;C:\Users\DELL\AppData\Local\Microsoft\WindowsApps;C:\Pr
ogram Files\Java\jdk-11.0.8\bin;C:\Program Files\JetBrains\IntelliJ IDEA
Community Edition
2020.3.1\bin;C:\Users\DELL\AppData\Local\atom\bin;C:\xampp\php;C:\Pr
ogram
Files\Java\jdk-16.0.1\bin;C:\Users\DELL\AppData\Roaming\npm;%USERPR
OFILE%\AppData\Local\Microsoft\WindowsApps;
[PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
[PROCESSOR_ARCHITECTURE] => x86
[PROCESSOR_ARCHITEW6432] => AMD64
[PROCESSOR_IDENTIFIER] => Intel64 Family 6 Model 58 Stepping 9,
GenuineIntel
[PROCESSOR_LEVEL] => 6
[PROCESSOR_REVISION] => 3a09
[ProgramData] => C:\ProgramData
[ProgramFiles] => C:\Program Files (x86)
[ProgramFiles(x86)] => C:\Program Files (x86)
[ProgramW6432] => C:\Program Files
[PROMPT] => $P$G
[PSModulePath] =>
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
[PUBLIC] => C:\Users\Public
[PyCharm_Community_Edition] => C:\Program Files\JetBrains\PyCharm
Community Edition 2020.2.3\bin;
[SESSIONNAME] => Console
[SystemDrive] => C:
[SystemRoot] => C:\WINDOWS
[TEMP] => C:\Users\DELL\AppData\Local\Temp
[TMP] => C:\Users\DELL\AppData\Local\Temp
[USERDOMAIN] => PBELVALKAR
[USERDOMAIN_ROAMINGPROFILE] => PBELVALKAR
[USERNAME] => DELL
[USERPROFILE] => C:\Users\DELL
[VS110COMNTOOLS] => C:\Program Files (x86)\Microsoft Visual Studio
11.0\Common7\Tools\
[windir] => C:\WINDOWS
[PHP_SELF] => globalvariable.php
[SCRIPT_NAME] => globalvariable.php
[SCRIPT_FILENAME] => globalvariable.php
[PATH_TRANSLATED] => globalvariable.php
[DOCUMENT_ROOT] =>
[REQUEST_TIME_FLOAT] => 1624079069.1329
[REQUEST_TIME] => 1624079069
[argv] => Array
(
[0] => globalvariable.php
)

[argc] => 1
)

GLOBALS => Array


(
[_GET] => Array
(
)
[_POST] => Array
(
)

[_COOKIE] => Array


(
)

[_FILES] => Array


(
)

[argv] => Array


(
[0] => globalvariable.php
)

[argc] => 1
[_SERVER] => Array
(
[ALLUSERSPROFILE] => C:\ProgramData
[APPDATA] => C:\Users\DELL\AppData\Roaming
[CLASSPATH] => C:\Program Files\Apache Software
Foundation\Tomcat 9.0\lib\servlet-api.jar;C:\Program
Files\Java\jdk1.8.0_281\bin;C:\Program Files\Java\jre1.8.0_281\bin;
[CommonProgramFiles] => C:\Program Files (x86)\Common Files
[CommonProgramFiles(x86)] => C:\Program Files (x86)\Common
Files
[CommonProgramW6432] => C:\Program Files\Common Files
[COMPUTERNAME] => PBELVALKAR
[ComSpec] => C:\WINDOWS\system32\cmd.exe
[DriverData] => C:\Windows\System32\Drivers\DriverData
[FPS_BROWSER_APP_PROFILE_STRING] => Internet Explorer
[FPS_BROWSER_USER_PROFILE_STRING] => Default
[HOMEDRIVE] => C:
[HOMEPATH] => \Users\DELL
[IntelliJ_IDEA_Community_Edition] => C:\Program
Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.1\bin;
[JAVA_HOME] => C:\Program Files\Java\jdk-11.0.8
[LOCALAPPDATA] => C:\Users\DELL\AppData\Local
[LOGONSERVER] => \\PBELVALKAR
[NUMBER_OF_PROCESSORS] => 4
[OneDrive] => C:\Users\DELL\OneDrive
[OS] => Windows_NT
[Path] => C:\Program Files\Common
Files\Oracle\Java\javapath;C:\Program Files (x86)\Common
Files\Oracle\Java\javapath;C:\Windows\system32;C:\Program
Files\Java\jdk-11.0.8\bin;C:\Program
Files\dotnet\;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\WI
NDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WIND
OWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\Open
SSH\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files
(x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files
(x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program
Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files
(x86)\Brackets\command;C:\Program Files\Apache Software
Foundation\Tomcat 9.0\lib\servlet-api.jar;C:\Program Files (x86)\Microsoft
SQL Server\90\Tools\binn\;C:\xampp\php;C:\Program
Files\Java\jdk-16.0.1\bin;C:\Program Files\nodejs\;C:\Program
Files\MySQL\MySQL Shell
8.0\bin\;C:\Users\DELL\AppData\Local\Programs\Python\Python38\Script
s\;C:\Users\DELL\AppData\Local\Programs\Python\Python38\;C:\Users\DE
LL\.dotnet\tools;C:\Users\DELL\AppData\Local\Programs\Microsoft VS
Code\bin;C:\Program Files\JetBrains\PyCharm Community Edition
2020.2.3\bin;C:\Users\DELL\AppData\Local\Microsoft\WindowsApps;C:\Pr
ogram Files\Java\jdk-11.0.8\bin;C:\Program Files\JetBrains\IntelliJ IDEA
Community Edition
2020.3.1\bin;C:\Users\DELL\AppData\Local\atom\bin;C:\xampp\php;C:\Pr
ogram
Files\Java\jdk-16.0.1\bin;C:\Users\DELL\AppData\Roaming\npm;%USERPR
OFILE%\AppData\Local\Microsoft\WindowsApps;
[PATHEXT] =>
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
[PROCESSOR_ARCHITECTURE] => x86
[PROCESSOR_ARCHITEW6432] => AMD64
[PROCESSOR_IDENTIFIER] => Intel64 Family 6 Model 58 Stepping 9,
GenuineIntel
[PROCESSOR_LEVEL] => 6
[PROCESSOR_REVISION] => 3a09
[ProgramData] => C:\ProgramData
[ProgramFiles] => C:\Program Files (x86)
[ProgramFiles(x86)] => C:\Program Files (x86)
[ProgramW6432] => C:\Program Files
[PROMPT] => $P$G
[PSModulePath] =>
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
[PUBLIC] => C:\Users\Public
[PyCharm_Community_Edition] => C:\Program
Files\JetBrains\PyCharm Community Edition 2020.2.3\bin;
[SESSIONNAME] => Console
[SystemDrive] => C:
[SystemRoot] => C:\WINDOWS
[TEMP] => C:\Users\DELL\AppData\Local\Temp
[TMP] => C:\Users\DELL\AppData\Local\Temp
[USERDOMAIN] => PBELVALKAR
[USERDOMAIN_ROAMINGPROFILE] => PBELVALKAR
[USERNAME] => DELL
[USERPROFILE] => C:\Users\DELL
[VS110COMNTOOLS] => C:\Program Files (x86)\Microsoft Visual
Studio 11.0\Common7\Tools\
[windir] => C:\WINDOWS
[PHP_SELF] => globalvariable.php
[SCRIPT_NAME] => globalvariable.php
[SCRIPT_FILENAME] => globalvariable.php
[PATH_TRANSLATED] => globalvariable.php
[DOCUMENT_ROOT] =>
[REQUEST_TIME_FLOAT] => 1624079069.1329
[REQUEST_TIME] => 1624079069
[argv] => Array
(
[0] => globalvariable.php
)

[argc] => 1
)

[GLOBALS] => Array


*RECURSION*
[x] => 54
[nm] => A B Shinde
[perr] => 54.345
[valuess] => Array
*RECURSION*
[keyy] => GLOBALS
)

x => 54
nm => A B Shinde
perr => 54.345
valuess => 54.345
keyy => values
ASSINGMENT9
Q9)Write a program to create an indexed array. Calculate sum and average.

⮚ Code:-
<?php
$a=array(13.2,21.5,32.1,45.2,51.2);
print_r($a);
$n=count($a);
$s=0;
$Av=0;
foreach($a as $b => $v){
echo "<br>";
echo "\n$b => $v";
$s=$s+$v;
}
$Av=$s/$n;
echo "<br>";
print("\n Sum = $s");
echo "<br>";
print("\nAverage = $Av");
?>

⮚ Outputs:-
C:\xampp\htdocs\PhpProject1\phpassingment>php Question9.php

Array
(
[0] => 13.2
[1] => 21.5
[2] => 32.1
[3] => 45.2
[4] => 51.2
)

0 => 13.2
1 => 21.5
2 => 32.1
3 => 45.2
4 => 51.2
Sum = 163.2
Average = 32.64

Assingment10:-
Q10)Write a program to create an associative array, where index is class_name
and value number of student in it.Display array using print_r and foreach loop.

⮚ Code:-
<?php
$a=array("FYBCS"=>112,"SYBCS"=>101,"TYBCS"=>67);
print_r($a);
echo "<br>";
foreach($a as $b => $v)
{
echo "\n$b => $v, $a[$b]";
echo "<br>";
}
?>
⮚ Outputs:-
C:\xampp\htdocs\PhpProject1\phpassingment>php Question10.php
Array
(
[FYBCS] => 112
[SYBCS] => 101
[TYBCS] => 67
)

FYBCS => 112, 112


SYBCS => 101, 101
TYBCS => 67, 67

Assingment11
Q11)Write a program to create and display 2d jagged array and print it using
print_r,forloop,foreach loop.Display their array of keys and array of values.

⮚ Code:-
<?php

$arr=array(array(1,4,5,5,1,2),array(11,24,51),array(41,42),array(31,42,15,3
2,11));
print_r($arr);
echo "<br>";
$r=0;
$c=0;
for($r=0;$r<count($arr);$r++)
{
for($c=0;$c<count($arr[$r]);$c++)
echo $arr[$r][$c]." ";
echo "<br>";
}
foreach($arr as $k => $v)
{
foreach($v as $k1 => $v1)
echo $v1." ";
echo "\n";
}
echo "\n\n Array of Keys : ";
echo"<br>";
print_r(array_keys($arr));
echo"<br>";
echo "\n\n Array of Values : ";
print_r(array_values($arr));
?>

⮚ Outputs:-
C:\xampp\htdocs\PhpProject1\phpassingment>php Question11.php
Array
(
[0] => Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 5
[4] => 1
[5] => 2
)

[1] => Array


(
[0] => 11
[1] => 24
[2] => 51
)

[2] => Array


(
[0] => 41
[1] => 42
)

[3] => Array


(
[0] => 31
[1] => 42
[2] => 15
[3] => 32
[4] => 11
)

)
1 4 5 5 1 2 11 24 51 41 42 31 42 15 32 11 1 4 5 5 1 2
11 24 51
41 42
31 42 15 32 11

Array of Keys : Array


(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
Array of Values : Array
(
[0] => Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 5
[4] => 1
[5] => 2
)

[1] => Array


(
[0] => 11
[1] => 24
[2] => 51
)

[2] => Array


(
[0] => 41
[1] => 42
)

[3] => Array


(
[0] => 31
[1] => 42
[2] => 15
[3] => 32
[4] => 11
)

ASSINGMENT12
Q12)Write a program to demonstrate sorting techniques in php using appropriate
inputs for sort(),rssort(),kssort(),assort(),arsort().

⮚ Code:-
<?php
echo "Sort Value wise Ascending\n";
$a=array(11,23,54,10,3,6,2);
print_r($a);
sort($a);
print_r($a);
echo"<br>";

echo "Sort Value wise Descending\n";


$a=array(11,23,54,10,3,6,2);
print_r($a);
rsort($a);
print_r($a);
echo"<br>";

echo "Sort Key wise Ascending\n";


$a=array();
$a[2]=43;
$a[100]=55;
$a[10]=23;
$a[1]=54;
print_r($a);
ksort($a);
print_r($a);
echo"<br>";

echo "Sort Key wise Descending\n";


$a=array();
$a[2]=43;
$a[100]=55;
$a[10]=23;
$a[1]=54;
print_r($a);
krsort($a);
print_r($a);
echo"<br>";

echo "Sorting Value Wise without changing keys:Ascending\n";


$S=array("ABC"=>65,"XYZ"=>34,"PQR"=>97);
print_r($S);
asort($S);
print_r($S);
echo"<br>";
echo "Sorting Value Wise without changing keys:Descending\n";
$S=array("ABC"=>65,"XYZ"=>34,"PQR"=>97);
print_r($S);
arsort($S);
print_r($S);
?>

⮚ Outputs:-
C:\xampp\htdocs\PhpProject1\phpassingment>php Question12.php
Sort Value wise Ascending
Array
(
[0] => 11
[1] => 23
[2] => 54
[3] => 10
[4] => 3
[5] => 6
[6] => 2
)
Array
(
[0] => 2
[1] => 3
[2] => 6
[3] => 10
[4] => 11
[5] => 23
[6] => 54
)
Sort Value wise Descending
Array
(
[0] => 11
[1] => 23
[2] => 54
[3] => 10
[4] => 3
[5] => 6
[6] => 2
)
Array
(
[0] => 54
[1] => 23
[2] => 11
[3] => 10
[4] => 6
[5] => 3
[6] => 2
)
Sort Key wise Ascending
Array
(
[2] => 43
[100] => 55
[10] => 23
[1] => 54
)
Array
(
[1] => 54
[2] => 43
[10] => 23
[100] => 55
)
Sort Key wise Descending
Array
(
[2] => 43
[100] => 55
[10] => 23
[1] => 54
)
Array
(
[100] => 55
[10] => 23
[2] => 43
[1] => 54
)
Sorting Value Wise without changing keys:Ascending
Array
(
[ABC] => 65
[XYZ] => 34
[PQR] => 97
)
Array
(
[XYZ] => 34
[ABC] => 65
[PQR] => 97
)
Sorting Value Wise without changing keys:Descending
Array
(
[ABC] => 65
[XYZ] => 34
[PQR] => 97
)
Array
(
[PQR] => 97
[ABC] => 65
[XYZ] => 34
)
ASSINGMENT13
Q13)Write a program using a user defined function to calculate factorial of

A given integer number and input and output strictly integer.

⮚ Code:-
<body>
<form method="post">
Enter a Number :
<input type="number" name="num"><br><br>
<input type="submit" name="sub" value="SUBMIT">
</form>
<?php
if(isset($_POST['sub']))
{
function Factorial($number)
{
if($number <= 1)
{
return 1;
}
else
{
return $number * Factorial($number - 1);
}
}
$number = $_POST['num'];
$fact = Factorial($number);
echo "Factorial = $fact";
}
?>
</body>
</html>

⮚ Output:-

ASSINGMENT14:-
Q14)Write a program using user defined function to interchange values of two
variables with the help of call by reference.

⮚ Code:-
<html>
<body>
<form method="post" action="Question14.php">
Enter a First Number :
<input type="Number1" name="num1"><br><br><br>
Enter a Second Number :
<input type="Number2" name="num2"><br><br><br>
<input type="submit" name="sub" value = "SUBMIT">
</form>
<?php
if(isset($_POST['sub']))
{
function swap(&$first , &$second)
{
$temp = $first;
$first = $second;
$second = $temp;
echo "After Swaping "." <br>";
echo "First number ".$first." Second number
".$second."<br>";
}

$a = $_POST['num1'];
$b = $_POST['num2'];

swap($a,$b);
echo "Before Swaping "." <br>";
echo "First number ".$b." Second number ".$a."<br>";
}
?>

⮚ Outputs:-
ASSINGMENT15
Q15)Write a program to write a callable function having strictly 2 integer input
and integer output, return maximum between them.

⮚ Code:-
<html>
<body>
<form method="post">
Enter a first number:
<input type="number" name="val1" /><br><br>
Enter a second number:
<input type="number" name="val2" /><br><br>

<input type="submit" name="typ" value="Maximum">

</form>
<?php
if(isset($_POST['typ']))
{ function maxi(&$n1,&$n2)
{
echo"maximum number is=". max($n1,$n2);
}
$n1=$_POST['val1'];
$n2=$_POST['val2'];
maxi($n1,$n2);
}

?>
</body>
</html>

⮚ Output:-

ASSINGMENT16
Q16)Write a program using exception handling to check given marks are between
0 and 100 or not, if not raise exception.

⮚ Code:-
<html>
<body>
<form method="post">
Enter first subject marks:
<input type="number" name="mark1" /><br><br>
Enter second subject marks:
<input type="number" name="mark2" /><br><br>
Enter third subject marks:
<input type="number" name="mark3" /><br><br>
Enter fourth subject marks:
<input type="number" name="mark4" /><br><br>
Enter fifth subject marks:
<input type="number" name="mark5" /><br><br>
<input type="submit" name="per" value="Check">

</form>
<?php
if(isset($_POST['per']))
{
$m1 = $_POST['mark1'];
$m2= $_POST['mark2'];
$m3= $_POST['mark3'];
$m4 = $_POST['mark4'];
$m5 = $_POST['mark5'];
try{
if ($m1>=0 && $m1<=100 && $m2>=0 && $m2<=100 && $m3>=0 &&
$m3<=100 && $m4>=0 && $m4<=100 && $m5>=0 && $m5<=100)
{
echo"marks are in between 0 to 100";
}

else
{
throw new Exception();
}

}
catch(Exception $e){
echo"marks are not in between 0 to 100////exception occured";
}
}
?>
</body>
</html>

⮚ Output:-

You might also like