You are on page 1of 4

Practical-6: Write a menu driven program to implement a calculator which performs only addition,

subtraction, multiplication and division. The operation should happen based on user choice.

<!DOCTYPE html>

<head>
<title>Simple Calculator Program in PHP - Tutorials Class</title>
</head>

<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
case "Multiply":
$result = $first_num * $second_num;
break;
case "Divide":
$result = $first_num / $second_num;
}
}

?>

<body>
<div id="page-wrap">
<h1>PHP - Simple Calculator Program</h1>
<form action="" method="post" id="quiz-form">
<p>
<input type="number" name="first_num" id="first_num" required="required" value="<?php
echo $first_num; ?>" /> <b>First Number</b>
</p>
<p>
<input type="number" name="second_num" id="second_num" required="required" value="<?
php echo $second_num; ?>" /> <b>Second Number</b>
</p>
<p>
<input readonly="readonly" name="result" value="<?php echo $result; ?>"> <b>Result</b>
</p>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />
</form>
</div>
</body>
</html>

Practical-7: Write a program sort ten number by using array.


<!--Write a program sort ten number by using array-->
<!DOCTYPE html>
<html>
<body>

<?php
$numbers = array(40, 61, 2, 22, 13,100,98,76,25,17);
sort($numbers);

$arrlength = count($numbers);
echo"Numbers in ascending order are:";
echo "<br>";
for($x = 0; $x < $arrlength; $x++) {

echo $numbers[$x];
echo "<br>";

}
?>

</body>
</html>

Practical-8: Write a program to demonstrate the concept of associative array.

<?php
/* First method to create an associate array. */
$student1 = array("C"=>95, "C++"=>90,
"Java"=>96, "Python"=>93,
"php"=>98);

/* Second method to create an associate array. */


$student2["DS"] = 95;
$student2["DBMS"] = 90;
$student2["OS"] = 96;
$student2["SE"] = 93;
$student2["COA"] = 98;

/* Accessing the elements directly */


echo "Marks for student one is:\n";
echo"<br>";
echo "DS:" . $student2["DS"], "\n";
echo "DBMS:" . $student2["DBMS"], "\n";
echo "OS:" . $student2["OS"], "\n";
echo "SE:" . $student2["SE"], "\n";
echo "Java:" . $student1["Java"], "\n";
?>

Practical-9: Write a program to demonstrate the concept of multidimensional array.

<?php

// PHP code to create


// multidimensional array

// Creating multidimensional
// associative array
$marks = array(

// Mahesh will act as key


"Mahesh" => array(

// Subject and marks are


// the key value pair
"C" => 95,
"C++" => 85,
"Java" => 74,
),

// Ram will act as key


"Ram" => array(

// Subject and marks are


// the key value pair
"C" => 78,
"C++" => 98,
"Java" => 46,
),

// Raghav will act as key


"Raghav" => array(

// Subject and marks are


// the key value pair
"C" => 88,
"C++" => 46,
"Java" => 99,
),
);

// Accessing the array element


// using dimensions

// It will display the marks of


// Mahesh in C subject
echo $marks['Mahesh']['C'] . "\n";
echo"<br>";

// Accessing array elements using for each loop


foreach($marks as $mark) {
echo $mark['C']. " ".$mark['C++']." ".$mark['Java']."<br>";
}

?>

You might also like