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

java report

The document provides an overview of JavaScript and PHP, detailing their features, importance, and applications. It includes multiple example programs for JavaScript, showcasing various functionalities such as simple interest calculation, number validation, and form handling. Additionally, it presents PHP programs for database operations like creating a connection, inserting, updating, and deleting data.

Uploaded by

aiagento1011
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)
11 views

java report

The document provides an overview of JavaScript and PHP, detailing their features, importance, and applications. It includes multiple example programs for JavaScript, showcasing various functionalities such as simple interest calculation, number validation, and form handling. Additionally, it presents PHP programs for database operations like creating a connection, inserting, updating, and deleting data.

Uploaded by

aiagento1011
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/ 35

Javascript

Javascript is standard client side scripting programming language for developing dynamic web
based applications. It is a client sensitive interpreted programming language with object
oriented capabilities. It was developed by Netscape and originally called LiveScript 1995.

Major features of Javascript


 It is a standard scripting language and supported by most of the browsers.
 It is a lightweight and efficient interpreted programming language.
 It is complementary to and integrated with HTML and Java.
 It is object oriented event driven programming language.
 It is open source and cross platform (independent to OS)

Importance of Javascript
 It is designed for solving client side application with prompt responds.
 It can dynamically modify webpage as per requirements.
 It can validate and respond user’s input.
 It does not require any interaction to web server while processing scripts on web browsers.

Page | 56
1. Javascript program to calculate simple intrest.

<html>
<head>
<title> Simple Intrest </title>
</head>
<body>
<script type="text/javascript">
var p=parseInt(prompt("Enter principle:"));
var t=parseInt(prompt("Enter time"));
var r=parseInt(prompt("Enter rate"));
var i;
i=(p*t*r)/100;
document.write("Simple Intrest=",+i);
</script>
</body>
</html>
OUTPUT :

Page | 57
2. Javascript program to find the entered number odd or even.
<html>
<head>
<title> Odd Even </title>
</head>
<body>
<script type="text/javascript">
var a=parseInt(prompt("Enter a number:"));
if (a%2==0)
document.write("Even number=",+a);
else
document.write("Odd number=",+a);
</script>
</body>
</html>
OUTPUT:

Page | 58
3. Javascript program to find factorial of the entered number.
<html>
<head>
<title> Odd Even </title>
</head>
<body>
<script type="text/javascript">
var count=1;
var f=1,i,n;
var n=parseInt(prompt("Enter a number:"));
for (i=1;i<n;i++)
f=f*i;
document.write("The factorial is =",+f);
</script>
</body>
</html>
OUTPUT:

Page | 59
4. Javascript program to find largest among 3 numbers.
<html>
<body>
<script type="text/javascript">
var a=parseInt(prompt("Enter first number:"));
var b=parseInt(prompt("Enter second number:"));
var c=parseInt(prompt("Enter third number:"));
if (a>b && a>c)
document.write("Largest number:",+a);
else if (b>c)
document.write("Largest number:",+b);
else
document.write("Largest number:",+c);
</script>
</body>
</html>
OUTPUT:

Page | 60
5. Javascript program to display even numbers upto 20.
<html>
<body>
<script type="text/javascript">
var a;
for (a=2;a<=20;a++)
{
if (a%2==0)
{
document.write(a+"<br>");
}
}
</script>
</body>
</html>
OUTPUT:

Page | 61
6. Javascript program to check whether the number is prime or
composite using function.
<html>
<head>
<title>Prime Check</title>
</head>
<body>
<script type="text/javascript">
var n = parseInt(prompt("Enter a number:"));
if (n <= 1)
{
document.write(n + " is not a Prime number.<br>");
}
else
{
var i;
for (i = 2; i < n; i++)
{
if (n % i === 0)
{
document.write(n + " is a Composite number.<br>");
break;
}
}
if (i === n)
{
document.write(n + " is a Prime number.<br>");
}
}

Page | 62
</script>
</body>
</html>
OUTPUT:

Page | 63
7. Javascript program to calculate area of reactangle.
<html>
<body>
<script type="text/javascript">
function area(l,b)
{
return l*b;
}
var l=prompt("Enter length:");
var b=prompt("Enter breadth:");
var a=area(l,b);
document.write("Area=",+a);
</script>
</body>
</html>
OUTPUT:

Page | 64
8. Javascript program to find reverse of the entered number.
<html>
<head>
<title>Reverse Number</title>
</head>
<body>
<script type="text/javascript">
function r(n)
{
var rev = 0;
while (n > 0)
{
var d = n % 10;
rev = rev * 10 + d;
n = parseInt(n / 10);
}
return rev;
}
var n = parseInt(prompt("Enter a number:"));
var rev = r(n);
document.write("The reverse of the number is: " + rev + "<br>");
</script>
</body>
</html>
OUTPUT:

Page | 65
9. Javascript program to find if the entered number is palindrome
or not.
<!DOCTYPE html>
<html>
<head>
<title>Palindrome Check</title>
</head>
<body>
<script type="text/javascript">
function p(n)
{
var rev = 0, temp = n;
while (n > 0)
{
var d = n % 10;
rev = rev * 10 + d;
n = parseInt(n / 10);
}
return rev === temp;
}
var n = parseInt(prompt("Enter a number:"));
if (p(n))
{
document.write(n + " is a Palindrome.<br>");
}
else
{
document.write(n + " is not a Palindrome.<br>");
}

Page | 66
</script>
</body>
</html>
OUTPUT:

Page | 67
10. Javascript program to check if the entered number is
Armstrong or not.
<html>
<body>
<script type="text/javascript">
var n,r,s=0,z;
var n=prompt("Enter any number:");
while (n!=0)
{
r=n%10;
s=s+r^3;
n=n/10;
}
if (z==s)
document.write("Armstrong");
else
document.write("Not armstrong");
</script>
</body>
</html>
OUTPUT:

Page | 68
11. Javascript program to print largest and smallest number
among 10 entered number in array.
<html>
<body>
<script type="text/javascript">
var a=[];
var smallest;
var largest;
var i;
for (i=0;i<10;i++)
{
a[i]=parseInt(prompt("Enter elements:"+(i+1)));
}
smallest=a[0];
largest=a[0];
for (i=1;i<10;i++)
{
if (a[i]<smallest)
{
smallest=a[i];
}
else if (a[i]>largest)
{
largest=a[i];
}
}
document.write("Smallest:"+smallest+"<br>");
document.write("Largest:"+largest);
</script>

Page | 69
</body>
</html>
OUTPUT:

Page | 70
12. Javascript program to display the multiplication table of
entered number.
<html>
<body>
<script type="text/javascript">
var n=prompt("Enter a number:");
var i;
for (i=1;i<=10;i++)
{
document.write(n+"*"+i+"="+(n*i)+"<br>");
}
</script>
</body>
</html>
OUTPUT:

Page | 71
13. Javascript program to create an object.
<html>
<body>
<h2>JavaScript Object</h2>
<script type="text/javascript">
var car={};
car.make="Toyota";
car.model="RAV4";
car.year=2022;
car.number="BA20PA-2020";
car.display=function(){
return this.make+"--"+this.model+"--"+this.year+"--"+this.number;
};
document.write(car.display()+"<br>");
</script>
</body>
</html>
OUTPUT:

Page | 72
14. Javascript program to add two numbers using javascript on
click event handeling concept.
<html>
<head>
<title>ONCLICK</title>
</head>
<body>
<script type="text/javascript">
function add()
{
var a, b, sum;
a = parseInt(prompt("Enter a number:"));
b = parseInt(prompt("Enter another number:"));
sum = a + b;
document.write("The sum is = " + sum);
}
</script>
<p>Click on the button to trigger</p>
<input type="button" onclick="add()" value="Click">
</body>
</html>
OUTPUT:

Page | 73
15. Javascript program for form validation.
<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
function validate() {
if (document.myForm.Name.value == "") {
alert("Please provide your name.");
document.myForm.Name.focus();
return false;
}
if (document.myForm.Email.value == "") {
alert("Please provide your email.");
document.myForm.Email.focus();
return false;
}
if (document.myForm.country.value == "-1") {
alert("Please select your country.");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="exg.php" name="myForm" onsubmit="return validate();">
<table cellspacing="2">
<tr>
<td align="right">Name</td>
Page | 74
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td align="right">Country</td>
<td>
<select name="country">
<option value="-1" selected>Choose Country</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="4">India</option>
</select>
</td>
</tr>
<tr>
<td align="right">Email</td>
<td><input type="text" name="Email" /></td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>

Page | 75
OUTPUT:

Page | 76
PHP
Php is a server side scripting language that is embedded in HTML. PHP originally stood for
Personal Home Pages, although the acronym is now understood to stand Hypertext
Preprocessor. It is used to manage dynamic content, databases, session training, even build
entire e commerce sites. It is integrated with a number of popular databases. PHP supports a
large number of major protocols such as POP3, IMAP.

FEATURES OF PHP
 This syntax of PHP is similar to C, so it is easy to learn and use.
 PHP runs efficiently on the server side.
 PHP supports a wide range of databases.
 It is compatible with almost all servers used today like Apaches, IIS, etc

APPLICATIONA OF PHP
 By using PHP, we can create, open, read, write and close files.
 PHP can handle forms, i.e gather data from the files, save data to a file, through email we
can send data, return data to the user.
 Access cookies variable and set cookies.
 It can encrypt data.

Page | 77
1. PHP program to create connection.
<html>
<body>
<?php
$servername="localhost";
$username="root";
$password=" ";
$conn= new mysqli($servername, $username, $password);
if ($conn->connect_error)
{
die("connection failed:" .mysqli_connect_error());
}
else
{
echo “<h2>Yes,connected</h2>";
}
?>
</body>
</html>

OUTPUT:

Page | 78
2. PHP program to create database.
<html>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$conn= new mysqli($servername,$username,$password);
if ($conn->connect_error)
{
die("connection failed:" .mysqli_connect_error());
}
else
{
echo "<h2>Yes,connected</h2>";
}
$sql="CREATE DATABASE myDB";
if ($conn->query($sql)===TRUE)
{
echo "<h2>Database created successfully</h2>";
}
else
{
echo "Error creating database:" .$conn->error;
}
$conn->close();
?>
</body>
</html>

Page | 79
OUTPUT:

Page | 80
3. PHP program to create database.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed!! " . $conn->connect_error);
} else
{
echo "Yes, connected<br>";
}
$sql = "CREATE TABLE office (
ID INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
FIRST_NAME VARCHAR(25),
LAST_NAME VARCHAR(30),
SALARY FLOAT
)";
if ($conn->query($sql) === TRUE)
{
echo "Table created successfully";
} else
{
echo "Error creating table: " . $conn->error;
}
Page | 81
$conn->close();
?>

OUTPUT:

Page | 82
4. PHP program to insert data into the table.
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="dB";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error)
{
die("Connection failed!!".$conn->connect_error);
}
else
{
echo"yes,connected<br>";
}
$sql="INSERT INTO office(ID,FIRST_NAME,LAST_NAME,SALARY)
VALUES (1,'Verma','Thakuri',5000)";
if($conn->query($sql)===TRUE)
{
echo"Inserting data successfully";
}
else
{
echo"Error in inserting data".$conn->error;
}
$conn->close();
?>
Page | 83
OUTPUT:

Page | 84
5. PHP program to delete the data from the file.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed!! " . $conn->connect_error);
} else
{
echo "Yes, connected<br>";
}

$sql = "DELETE FROM office WHERE ID=3";


if ($conn->query($sql) === TRUE)
{
echo "Record deleted successfully";
}
else
{
echo "Error in deleting data: " . $conn->error;
}
$conn->close();
?>

Page | 85
OUTPUT:

Page | 86
6. PHP program to update the data.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed!! " . $conn->connect_error);
} else
{
echo "Yes, connected<br>";
}
$sql="UPDATE office SET FIRST_NAME='John', LAST_NAME='Doe',
SALARY=75000 WHERE ID=3";
if($conn->query($sql)===TRUE)
{
echo"Record updated successfully";
}
else
{
echo"Error in updating data".$conn->error;
}
$conn->close();
?>

Page | 87
OUTPUT:

Page | 88
7. PHP program to fetch the data.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed!! " . $conn->connect_error);
} else
{
echo "Yes, connected<br>";
}
$sql="SELECT ID,FIRST_NAME,LAST_NAME,SALARY FROM office";
$result=$conn->query($sql);
if($result->num_rows>0)
{
while($row=$result->fetch_assoc())
{
echo"<br>ID:".$row["ID"]."__NAME:".$row["FIRST_NAME"]."
".$row["LAST_NAME"]."<br>";
}
}
else
{
echo"0 returns";

Page | 89
}
$conn->close();
?>

OUTPUT:

Page | 90

You might also like