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

Lab programs

The document contains a series of programming tasks and examples, primarily focusing on JavaScript and PHP. It includes instructions for creating a simple calculator, generating squares and cubes of numbers, and handling string manipulations, as well as PHP programs for displaying visitor counts and digital clocks. Additionally, it covers XML document creation for student information and matrix operations in PHP.

Uploaded by

athulatk6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab programs

The document contains a series of programming tasks and examples, primarily focusing on JavaScript and PHP. It includes instructions for creating a simple calculator, generating squares and cubes of numbers, and handling string manipulations, as well as PHP programs for displaying visitor counts and digital clocks. Additionally, it covers XML document creation for student information and matrix operations in PHP.

Uploaded by

athulatk6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Index

Sl No Programs Page No
01 Write a Javascript to design a simple calculator to perform the following 2-6
operations: sum, product, difference and quotient.

02 Write a Javascript that calculates the squares and cubes of the numbers 7-8
from 0 to 10 and outputs HTML text that displays the resulting values in
an HTML table formats.

03 Write a javascript program for Text-Growing and Text-Shrinking. 9-10

04 Write a HTML file and Javascript program for. 11-12


a) Position in the string of the leftmost vowel.
b) Number with its string in a reverse order.

05 XML document to store information about the student. 13-16

06 Write a php program to display number of visitors visiting the webpage. 17-18

07 Write a php program to display the digital clock with current time of the 19-20
server.
08 Write the PHP programs to do the following: 21-27
a) Implement simple calculator operations.
b) Find the transpose, Multiplication, Addition of two matrices.

09 Write a PHP program named states.py that declares a variable states 28-30
with value “Mississippi Alabama Texas Massachussetts Kansas”.Write a
PHP program that does the following.
a) Search for a word in variable states that ends in xas. Store this word
in element( ) of a list named states list.
b) Search for a word in states that begins with k and s .Perform a case-
insensitive comparison.
c) Search for a word in states that ends in a .Store this word in element 3
of the list.

10 Write a PHP program to sort the student records which are stored in the 31-34
database using selection sort.

1
201304
1. Write a Javascript to design a simple calculator to perform the
following operations: sum, product, difference and quotient.

<html>

<head>

<script>

//function that display value

function dis(val)

document.getElementById("result").value+=val

//function that evaluates the digits and returns the result

function solve()

let x = document.getElementById("result").value

let y = eval(x)

document.getElementById("result").value = y

//function that clear the display

function clr()

document.getElementById("result").value = ""

</script>

<!--for styling-->

2
201304
<style>

.title{

margin-bottom: 10px;

text-align: center;

width: 210px;

color: green;

border: solid black 2px;

input[type="button"]

background-color: green;

color: black;

border: solid black 2px;

width: 100%;

input[type="text"]

background-color: white;

border: solid black 2px;

width:100%;

</style>

3
201304
</head>

<!--creating a table-->

<body>

<div class="title">CALCULATOR</div>

<table border="1">

<tr>

<td colspan="3"><input type="text" id="result"/></td>

<!--clr() function will call clr to clear all the values-->

<td><input type="button" value="c" onclick="clr()"/></td>

</tr>

<tr>

<!--create button and assign value to each button-->

<!--dis("") will call function dis to display the value-->

<td><input type="button" value="1" onclick="dis('1')"/></td>

<td><input type="button" value="2" onclick="dis('2')"/></td>

<td><input type="button" value="3" onclick="dis('3')"/></td>

<td><input type="button" value="/" onclick="dis('/')"/></td>

</tr>

<tr>

<td><input type="button" value="4" onclick="dis('4')"/></td>

<td><input type="button" value="5" onclick="dis('5')"/></td>

<td><input type="button" value="6" onclick="dis('6')"/></td>

4
201304
<td><input type="button" value="-" onclick="dis('-')"/></td>

</tr>

<tr>

<td><input type="button" value="7" onclick="dis('7')"/></td>

<td><input type="button" value="8" onclick="dis('8')"/></td>

<td><input type="button" value="9" onclick="dis('9')"/></td>

<td><input type="button" value="+" onclick="dis('+')"/></td>

</tr>

<tr>

<td><input type="button" value="." onclick="dis('.')"/></td>

<td><input type="button" value="0" onclick="dis('0')"/></td>

<!--solve function call function solve to evaluate value-->

<td><input type="button" value="=" onclick="solve()"/></td>

<td><input type="button" value="*" onclick="dis('*')"/></td>

</tr></table>

</body>

</html>

5
201304
Output:

6
201304
2. Write a Javascript that calculates the squares and cubes of the numbers
from 0 to 10 and outputs HTML text that displays the resulting values in
an HTML table formats.

<html>

<head>

<title>Sqaures and Cubes of the number</title></head>

<script>

document.write('<p><b>Squares and Cubes of the number from 1-10</b></p>');

document.write('<table border="2" cellspacing="2">');

document.write('<th> NUMBER </th> <th> SQUARE </th> <th> CUBE </th>');

for(var i=1; i<=10; i++)

document.write("<tr><td>" + i + "</td> "+"<td>" + i * i + "</td>" + "<td>" + i * i * i

+ "</td></tr>");

document.write("</table>");

</script>

</html>

7
201304
Output:

8
201304
3. Write a Javascript program for Text-Growing and Text-Shrinking.

<html>

<body>

<div id="h"></div>

<script>

var v = 0,f = 1,t = "TEXT-GROWING", color;

function a(){

if(f==1) v+=5, color="red";else v-=5,color="blue";

document.getElementById("h").innerHTML= "<h1 style=\"font-size: "+v+"px ;


margin: 0px; color:

"+color+"\"><b>"+t+"</b></h1>";

if(v==50) f = 0,t="TEXT-SHRINKING";

if(v==5) f = 1,t="TEXT-GROWING";

c();

function c(){

setTimeout(a,300);}

c();

</script>

</body>

</html>

9
201304
Output:

10
201304
4. Write a HTML file and Javscript program for.
a) Position in the string of the leftmost vowel.
b) Number with its string in a reverse order.

<html>

<script>

var a = prompt("Enter the query"),b = parseInt(a),z=0;

if(b){

while(b>0)

var r=b%10, z= z*10+r, b = Math.floor(b/10);

document.write("Entered query: "+a+" <br> given number in reverse order:"+z);

else{

a = a.search('[aeiouAEIOU]');

document.write("The first occurance of vowel is at:"+ (a+1));

</script>

</html>

11
201304
Output:

12
201304
5. XML document to store information about the student.

XML file

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/css" href="la5.css"?>

<STUDENTDTA>

<STUDENT>

<USN>USN: 3GN16CS002</USN>

<NAME>NAME: SAMMISHRA</NAME>

<COLLEGE>COLLEGE: GNDECB</COLLEGE>

<BRANCH>BRANCH: BVOC</BRANCH>

<YEAR>YEAR:2021</YEAR>

<EMAIL>EMAIL: [email protected]</EMAIL>

</STUDENT>

<STUDENT>

<USN>USN: 3GN16CS009</USN>

<NAME>NAME: PRERANA</NAME>

<COLLEGE>COLLEGE: GNDECB</COLLEGE>

<BRANCH>BRANCH: BVOC</BRANCH>

<YEAR>YEAR:2020</YEAR>

13
201304
<EMAIL>EMAIL: [email protected]</EMAIL>

</STUDENT>

<STUDENT>

<USN>USN: 3GN16CS010</USN>

<NAME>NAME: NEHA</NAME>

<COLLEGE>COLLEGE: GNDECB</COLLEGE>

<BRANCH>BRANCH: BVOC</BRANCH>

<YEAR>YEAR:2021</YEAR>

<EMAIL>EMAIL: [email protected]</EMAIL>

</STUDENT>

<STUDENT>

<USN>USN: 3GN16CS019</USN>

<NAME>NAME: SAHITHYA</NAME>

<COLLEGE>COLLEGE: GNDECB</COLLEGE>

<BRANCH>BRANCH: BVOC</BRANCH>

<YEAR>YEAR:2021</YEAR>

<EMAIL>EMAIL: [email protected]</EMAIL>

</STUDENT>

</STUDENTDTA>

14
201304
la5.css

*{

display: block; font-size: 20px;

USN{

color: blue;

font-size: 30px;

margin-top: 20px;

15
201304
Output:

16
201304
6. Writea php program to display number of visitors visiting the webpage
and to display this count of visitors , with proper headings.

<?php

echo "<h1> refresh page </h1>";

$file = 'count.txt';

$c = file_get_contents($file);

file_put_contents($file,$c+1);

echo "the number of users visited:".$c;

?>

17
201304
Output:

18
201304
7. Writea php program to display a digital clock which displays the
current time of the server.

<head>

<meta http-equiv="refresh" content="1"/>

<style>

p{

color: yellow;

font-size: 90px;

position: absolute;

top: 40%;

left:50%;

transform: translate(-50%,-50%);

body{

background-color: maroon;

</style>

<p><?php echo date("h: i : s A");?></p>

</head>

19
201304
Output:

20
201304
8a. Write the PHP programs to do the following:
-Implement simple calculator operations.

<html>

<head>

<style>

table, td, th {

border: 1px solid black;

width: 35%;

text-align: center;

background-color: lightgray;

table { margin: auto; }

input,p { text-align:right; }

</style>

</head>

<body>

<form method="post" action="prog8a.php">

<table>

<caption><h2> SIMPLE CALCULATOR </h2></caption>

<tr>

<td>First Number:</td><td><input type="text" name="num1"


/></td>

<td rowspan="2"><button type="submit" name="submit"

value="calculate">Calculate</td></tr>

<tr>

21
201304
<td>Second Number:</td><td><input type="text"
name="num2"/></td>

</tr>

</form>

<?php

if(isset($_POST['submit'])) // it checks if the input submit is filled

$num1 = $_POST['num1'];

$num2 = $_POST['num2'];

if(is_numeric($num1) and is_numeric($num2) )

echo "<tr><td>
Addition :</td><td><p>".($num1+$num2)."</p></td>";

echo "<tr><td> Subtraction :</td><td><p> ".($num1-


$num2)."</p></td>";

echo "<tr><td>

Multiplication :</td><td><p>".($num1*$num2)."</p></td>";

echo "<tr><td>Division :</td><td><p>


".($num1/$num2)."</p></td>";

echo "</table>";

Else

echo"<script> alert(' ENTER VALID NUMBER');</script>";

22
201304
}

?>

</body>

</html>

23
201304
Output:

24
201304
8b. Write the PHP programs to do the following:
Find the transpose, Multiplication, Addition of two matrices.

`<!DOCTYPE html>

<html>

<body>

<?php

function pr($a){

foreach ($a as $b) {

foreach ($b as $c) {

echo $c ." ";

}echo "<br>";

}echo "<br>";

$a = [[1,2,3],[4,5,6],[7,8,9]];

$b = [[7,8,9],[4,5,6],[1,2,3]];

echo "<b>First Matrix : </b><br>" ; pr($a);

echo "<b>Second Matrix : </b><br>"; pr($b);

for ($i=0; $i < 3; $i++)

for ($j=0; $j < 3; $j++)

$c[$i][$j] = $a[$j][$i];

echo "<b>Transpose Of First Matrix : </b><br>"; pr($c);

for ($i=0; $i < 3; $i++)

25
201304
for ($j=0; $j < 3; $j++)

$c[$i][$j] = $a[$i][$j] + $b[$i][$j];

echo "<b>Addition Of Two Matrix : </b><br>"; pr($c);

for ($i=0; $i < 3; $i++)

for ($j=0; $j < 3; $j++){

$c[$i][$j] = 0;

for ($k=0; $k < 3; $k++)

$c[$i][$j] += $a[$i][$k] * $b[$k][$j];

echo "<b>Multiplication Of Two Matrix : </b><br>"; pr($c);

?>

</body>

</html>

26
201304
Output:

27
201304
9. Write a PHP program named states.py that declares a variable states
with value “Mississippi Alabama Texas Massachussetts Kansas”.Write a
PHP program that does the following.
a) Search for a word in variable states that ends in xas. Store this word in
element( ) of a list named states list.
b) Search for a word in states that begins with k and s .Perform a case-
insensitive comparison.
c) Search for a word in states that ends in a .Store this word in element 3
of the lis

<html>

<body>

<?php

$states = "Mississippi Alabama Texas Massachusetts Kansas";

$b = explode(' ',$states);

echo "<br>ORIGINAL ARRAY :<br>";

foreach ( $b as $i => $value )

echo "states[$i] = $value<br>";

foreach ($b as $c)

$n = strlen($c);

if($c[$n-1]=='s' && $c[$n-2]=='a' && $c[$n-3]=='x')


$d[0] = $c;

if($c[0]=='K' && $c[$n-1]=='s') $d[1] = $c;

if($c[0]=='M' && $c[$n-1]=='s') $d[2] = $c;

if($c[$n-1]=='a') $d[3] = $c;

echo "<br>RESULTANT ARRAY :<br>";

28
201304
for ($i=0; $i < count($d); $i++)

echo "statesList[$i] = $d[$i]<br>";

?>

</body>

</html>

29
201304
Output:

30
201304
10. Write a PHP program to sort the student records which are stored in
the database using selection sort

<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "labprogram";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

$sql = "SELECT * FROM student";

$result = $conn->query($sql);

$usn = array() ;

echo "<table border='2'><caption>Before Sorting </caption><br>";

echo "<tr><th>USN</th><th>NAME</th><th>Marks</th></tr>";

if ($result->num_rows > 0)

while($row = $result->fetch_assoc())

echo "<tr><td>". $row["usn"]."</td>";

echo "<td>". $row["name"]."</td>";

echo "<td>". $row["marks"]."</td></tr>";

$usn[] = $row["usn"] ;

31
201304
}

$n = sizeof($usn) ;

for($i = 0 ; $i < $n-1 ; $i++ )

$pos = $i ;

for($j = $i + 1 ; $j < $n ; $j++ )

if( $usn[$pos] < $usn[$j])

$pos = $j ;

if( $pos != $i)

$temp = $usn[$i] ;

$usn[$i] = $usn[$pos] ;

$usn[$pos] = $temp ;

$name = [ ] ;

$marks = [ ] ;

$result = $conn->query($sql);

if ($result->num_rows> 0)

32
201304
while($row = $result->fetch_assoc())

for($i=0;$i<$n;$i++)

if($row["usn"] == $usn[$i])

$name[$i]=$row["name"];

$marks[$i]=$row["marks"];

echo "<br><table border='2'><caption>After Sorting </caption><br>";

echo "<tr><th>USN</th><th>NAME</th><th>Marks</th></tr>";

for($i = 0 ; $i < sizeof($usn) ; $i++)

echo "<tr><td>". $usn[$i]."</td>";

echo "<td>". $name[$i]."</td>";

echo "<td>". $marks[$i]."</td></tr>";

?>

33
201304
Output:

34
201304

You might also like