Professional Documents
Culture Documents
Group B
1. a. Write a Program to check and print whether a given number is even or
odd.
Code :
<html>
<body>
<form method="post">
Enter a number:
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
<?php
if($_POST){
$number = $_POST['number'];
//divide entered number by 2
//if the reminder is 0 then the number is even otherwise the number is odd
if(($number % 2) == 0){
echo "$number is an Even number";
}else{
echo "$number is Odd number";
}
}
?>
</body>
</html>
b. Write a program to compute net amount from the given quantity purchased
and rate per quantity. Discount @10% is allowed if the quantity purchased
exceeds 100.
Code :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>practical1b</title>
</head>
<body>
<form method="post">
Enter amount of purchase(10% discount above purchase of 100):
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
<?php
if($_POST){
$dis = 0;
$number= $_POST['number'];
if ($number>=100) {
$dis = $number*10/100;
echo "Congrats!!!You get discount of $dis";
$number = $number -$dis;
}
echo "</br>Final amount to paid: $number";
}
?>
</body>
</html>
2 a. Write a program to find the largest among three numbers using ternary
operators.
Code:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>practical2a</title>
</head>
<body>
<form method="post">
Enter first no:
<input type="number" name="number1">
Enter second no:
<input type="number" name="number2">
Enter third no:
<input type="number" name="number3">
<input type="submit" value="Submit">
</form>
<?php
if($_POST){
$n1 = $_POST['number1']; $n2 = $_POST['number2']; $n3 =
$_POST['number3'];
$max = ($n1 > $n2) ? ($n1 > $n3 ? $n1 : $n3) :
($n2 > $n3 ? $n2 : $n3);
echo "Largest number among ". $n1 . ", ". $n2 .
" and " . $n3. " is " .$max;
}
?>
</body>
</html>
b. Write a program to print the sum of digits of a given number. (using while loop)
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>practical2b</title>
</head>
<body>
<form method="post">
Enter a nuber to find sum of its digits:
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
<?php
if($_POST){
$x = $_POST['number'];
$sum = 0;
$m = NULL;
3. Write a program to enter numbers till the user wants. At the end it should display
the count of positive, negative and zeros entered. (Using do-while loop)
Code:
<?php
$arr = explode(' ', readline());
$i= count($arr);
$p=0;$n=0;$z=0;$j=0;
do
{
if($arr[$j]>0)
$p++;
else if ($arr[$j]==0)
$z++;
else
$n++;
$j++;
}while($j<$i);
echo "\n\n";
echo "Back from function : \n\n";
echo " After swapping strings are \n"."\n String 1 = ". $str1. "\n String 2 = ". $str2;
/Call by reference
//Pass by reference
<?php
$str1="Sahil";
$str2="Raheja";
echo "<br>Swapping of two strings using Call by reference \n\n";
echo "Before swapping strings are \n"."String 1 = ". $str1." \t String
2 = ".$str2."\n\n";
swap ($str1,$str2);
echo "\n\n";
echo "Back from function: \n\n";
echo "After swapping strings are\n"."\n String 1 = ".$str1."\t String 2
= ".$str2;
function swap(&$x,&$y)
{
$temp=$x;
$x=$y;
$y=$temp;
echo "In function:\n";
echo "After swapping strings are \n String 1 = ".$x."\t String 2 =
".$y." \n";
}
?>
Group C
1. Write a program that will accept an array of integers as input, and output an
array where for each item in the source array, the new array will perform the following
operations: a. For even numbers divide by 2 b. For odd numbers multiply by 3
Code:
<?php
$array = explode(' ', readline());
echo "Array before operation:";
print_r($array);
for ($i=0; $i<count($array); $i=$i+1)
{
if ($array [$i] %2==0)
$array [$i] =$array [$i]/2;
else
$array [$i] =$array[$i]*3;
}
echo "After operation array values are \n";
print_r($array);
?>
2. Create an associative array using the countries as keys, the cities as values and
transform it into 2- dimensional array and display the data as a table.
Code:
<html>
<head><title>Prac No. C2. Associative Array</title></head>
<body>
<?php
$places=array(
'Japan'=>'Osaka',
'Mexico'=>'Zapopan',
'USA'=>'Chicago',
'India'=>'Mumbai',
'South Korea'=>'Seoul',
'China'=>'Shanghai',
'Nigeria'=>'Lagos',
'Argentina'=>'Buenos Aires',
'Egypt'=>'Luxor',
'England'=>'Alston');
$multArr = array();
foreach ($places as $key => $value)
{
$ar = array();
$ar[0] = $key;
$ar[1] = $value;
array_push($multArr,$ar);
}
echo "<table border='1'>
<tr>
<th>Country</th>
<th>City</th>
</tr>";
foreach ($multArr as $val)
{
echo "<tr>";
echo "<td>" . $val[0] . "</td>";
echo "<td>" . $val[1] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
3. Write a program that creates a file and writes contents to it and displays it. Then
append some data to it.
Code:
<?php
// Writing Data In File
$myfile = fopen("myFile.txt", "w") or die("Error in opening file!");
$txt1 = "College Name : ";
fwrite($myfile, $txt1);
fclose($myfile);
echo "<h2>Date Written Successfully In File</h2>";
// Appending Data In File
$myfile = fopen("myFile.txt", "a") or die("Error in opening file!");
$txt2 = "Government College Of Engineering\n";
fwrite($myfile, $txt2);
fclose($myfile);
echo "<h2>Date Appended Successfully In File</h2>";
// Reading Data From File
$myfile = fopen("myFile.txt", "r") or die("Error in opening file!");
$txt3 = fread($myfile,filesize("myFile.txt"));
echo "<h2>Date From File : ".$txt3."</h2>";
fclose($myfile);
?>
4. Write a PHP program using Java Script to convert the decimal number to its
binary equivalent. You must use a form to accept the number from the user.
Code:
<html>
<body>
<b>Insert Decimal Number: </b><input type = "text" name = "deci" id =
"deci" size = "15" maxlength = "15" />
<input type="button" value="Convert!" onclick="dec2bin()" />
<br><br>
<div id = "result"></div>
<script type = "text/javascript">
function dec2bin() {
var x = document.getElementById("deci").value;
if ((/[^0-9]/g.test(x)) || x == "") {
alert ("You must enter an integer decimal number!");
document.getElementById("deci").value = "";
document.getElementById("deci").focus();
return false;
}
x = parseInt(x);
var bin = x.toString(2);
var figs = "The binary representation of " + x + " is " + bin + "<br>";
document.getElementById("result").innerHTML = figs;
}
</script>
</body>
</html>
5. Write a PHP code that defines the class Student with attributes RollNo, Name,
Branch, and Year, create 3 instances of it, sets the values of each instance appropriately and
print the values of all attributes.
Code:
<?php
class Student
{
public $RollNo;
public $Branch;
public $Name;
public $Year;
function getRollNo()
{return $this->RollNo;}
function getBranch()
{return $this->Branch;}
function getname()
{return $this->Name;}
function getYear()
{return $this->Year;}
function __construct()
{}
function __destruct()
{}
}
//Instances
$obj[1]= new Student();
$obj[2]= new Student();
$obj[3]= new Student();
$obj[1]->set(1001,"CSE","amit",2012);
$obj[2]->set(1002,"CSE","prashant",2011);
$obj[3]->set(1003,"ECE","sachin",2012);
for($j=1;$j<4;$j++)
{
echo $obj[$j]->getRollNo()."\t";
echo $obj[$j]->getBranch()."\t";
echo $obj[$j]->getName()."\t";
echo $obj[$j]->getYear()."\n";
}
?>