1. Write a program to JavaScript that illustrate event handling.
<html>
<head>
<title>qno1</title>
</head>
<body onload="alert('this is onload even')";>
<input type="button" value="onclickeventishere" onclick="onclickfun()" id="b1">
<input type="button" value="onmouseovereventishere" onmouseover="mover()" id="b2">
<input type="button" value="onmouseoutevenishere" onmouseout="mout()" id="b3">
<script>
function onclickfun() {
alert('this is onlicked event');
}
function mover() {
alert('this is mouse over event');
}
function mout() {
alert('this is mouseout event');
}
</script>
</body>
</html>
2. Write a JavaScript to display 1 to 10 using for loop, while loop, and do-while loop.
<script>
let i=1;
// using for loop
for(i=0; i<=10; i++){
document.write(i+" ");
}
// using while loop
while(i<=10){
document.write(i+" ");
i++;
}
//using do-while loop
do{
document.write(i+" ");
i++;
}while(i<=10)
</script>
3. Write a JavaScript program to calculate the factorial of a given number.
<script>
let fact = 1;
let i;
let num = prompt("enter the number");
num = parseInt(num);
for(i=1; i<=num; i++){
fact *=i;
}
document.write(fact);
</script>
4. Write a JavaScript program to input three numbers and find the largest one.
<script>
let firstnum =parseInt(prompt("enter the 1st number"));
let secondnum =parseInt(prompt("enter the 2nd number"));
let thirdnum =parseInt(prompt("enter the 3th number"));
if(firstnum >secondnum && firstnum>thirdnum){
document.write(firstnum + " this is the largest number");
}else if(secondnum > thirdnum){
document.write(secondnum + " is the largest number");
}else{
document.write(thirdnum + " is the largest number");
}
</script>
5. Write a program to input any three numbers and find the average in JavaScript.
<script>
let firstnum =parseInt(prompt("enter the 1st number"));
let secondnum = parseInt(prompt("enter the 2nd number"));
let thirdnum = parseInt(prompt("enter the 3rd number"));
let avg = (firstnum * secondnum * thirdnum)/3;
document.write("the average number is "+ avg);
</script>
6. Write a program to find area and circumferences of circle in JavaScript.
<script>
let x = parseInt(prompt("Enter radius"));
let y = x*x*22/7;
let cr = 2*x*22/7;
document.write("area of circle is "+y);
document.write("<br>circumference of circle is "+cr);
</script>
7. Write a program to display all even number from 1 to 100 in JavaScript.
<script>
let num=1;
while(num<=100){
if(num%2==0){
document.write(num +" ");
}
num++;
}
8. Write a program to sum of two number using function.
<script>
function sum(x, y){
let z=x+y;
document.write(z);
}
let num1 = parseInt(prompt("Enter the 1st Number"));
let num2 = parseInt(prompt("enter the 2nd number"));
sum(num1, num2);
</script>
9. Write a program to input any number and find its reverse order in JavaScript.
<!DOCTYPE html>
<html>
<head>
<title>Reverse Number</title>
</head>
<body>
<input id="num">
<button onclick="reverseNumber()">Enter</button>
<script>
function reverseNumber() {
let num = document.getElementById("num").value;
let reversedNumber = parseInt(num.toString().split('').reverse().join(''));
document.write("Reversed number: " + reversedNumber);
}
</script>
</body>
</html>
10. Write a program to display the Fibonacci series. 1 1 2 3 5 8 13 ……n in JavaScript.
<script>
let n,a=0,b=1,c,i;
n = parseInt(prompt("Enter the number"));
for(i=1;i<=n;i++){
document.write(a+" ");
c=a+b;
a=b;
b=c;
}
</script>
11. Design a form with username, address, e-mail, password, and submit button.
Validated the form using JavaScript.
<html>
<html>
<head>
<title>Form Validation</title>
<script>
function validateForm() {
var username = document.getElementById("username").value;
var address = document.getElementById("address").value;
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
if (username.trim() === "") {
alert("Please enter your username.");
return false;
}
if (address.trim() === "") {
alert("Please enter your address.");
return false;
}
if (email.trim() === "") {
alert("Please enter your email.");
return false;
}
var emailRegex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
if (!emailRegex.test(email)) {
alert("Please enter a valid email address.");
return false;
}
if (password.trim() === "") {
alert("Please enter your password.");
return false;
}
if (password.length < 6) {
alert("Password must be at least 6 characters long.");
return false;
}
return true;
}
</script>
</head>
<body>
<h2>Form Validation</h2>
<form onsubmit="return validateForm()">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<label for="address">Address:</label><br>
<input type="text" id="address" name="address"><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
13. Write a PHP program to check if a string is a palindrome or not.
<?php
function isPalindrome($str) {
$cleanedStr = strtolower(preg_replace("/[^A-Za-z0-9]/", "", $str));
$reversedStr = strrev($cleanedStr);
if ($cleanedStr === $reversedStr) {
return true;
} else {
return false;
}
}
$inputString = "A man, a plan, a canal, Panama";
if (isPalindrome($inputString)) {
echo "The string \"$inputString\" is a palindrome.";
} else {
echo "The string \"$inputString\" is not a palindrome.";
}
?>
14. Write a PHP code to enter any two number and display their sum, difference and
product.
<html>
<body>
<form method="post">
<input type="number" name="lenght">
<input type="number" name="breadth">
<input type="submit" name="area" value="area">
<input type="submit" name="perimeter" value="perimeter">
</form>
<?php
if(isset($_POST['area'])){
$l=$_POST['lenght'];
$b=$_POST['breadth'];
$a=$l*$b;
echo 'area is '.$a;
}
if(isset($_POST['perimeter'])){
$l=$_POST['lenght'];
$b=$_POST['breadth'];
$r=2*$l+2*$b;
echo 'perimeter is '.$r;
}
?>
</body>
</html>
15. Write a PHP code to find out area and perimeter of rectangle.
<?php
$length = 10;
$width = 5;
$area = $length * $width;
$perimeter = 2 * ($length + $width);
echo "Length of the rectangle: $length <br>";
echo "Width of the rectangle: $width <br>";
echo "Area of the rectangle: $area <br>";
echo "Perimeter of the rectangle: $perimeter <br>";
?>
16. Create a registration form including Name, Address, Phone No., email address,
Qualification, Gender and Country and get the values entered by user using post
method and display.
<html>
<body>
<form method="post">
Enter name:<input type="text" name="name">
Enter address:<input type="text" name="address">
Enter number:<input type="number" name="phone">
Enter email:<input type="text" name="email">
Enter qualification:<input type="text" name="qualification">
Enter gender:<input type="text" name="gender">
Enter countryname:<input type="text" name="country">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])){
$n=$_POST['name'];
$a=$_POST['address'];
$p=$_POST['phone'];
$e=$_POST['email'];
$q=$_POST['qualification'];
$g=$_POST['gender'];
$c=$_POST['country'];
echo 'name is ',$n,' address is ',$a,' phone is ',$p,' email is ',$e,' qualification is ',$q,' gender is '.$g.' country is ',$c;
}
?>
</body>
</html>
17. Write a program in PHP to display all the even numbers from 1 to 100.
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 2 == 0) {
echo $i . " ";
}
}
?>
18. Write a program in PHP to input a number and check it is odd and even.
<html>
<body>
<form method="post">
<input type="number" name="num">
<input type="submit" name="submit" value="check">
</form>
<?php
if (isset($_POST['submit'])) {
$n=$_POST['num'];
for($i=1;$i<=10;$i++){
$num=$i*$n;
echo $num.' ';
}
}
?>
</body>
</html>
19. Write a program in PHP to display the multiplication table of a number.
<html>
<body>
<form method="post">
<input type="number" name="num1">
<input type="submit" name="submit" value="find factorial">
</form>
<?php
if(isset($_POST['submit'])){
$n=$_POST['num1'];
$fac=1;
for($i=1;$i<$n;$i++){
$fac=$i*$fac+$fac;
}
echo 'the factorial is '.$fac;
}
?>
</form>
</body>
</html>
20. Write a program in PHP to input a number and find its Armstrong or not.
<html>
<body>
<form method="post">
<input method="number" name="num1">
<input type="submit" name="submit" value="check">
</form>
<?php
if(isset($_POST['submit'])){
$n=$_POST['num1'];
$mul=0;
$arm=$_POST['num1'];
while($n>0){
$rem=$n%10;
$mul=$mul+$rem*$rem*$rem;
$n=$n/10;
}
if($mul==$arm){
echo 'its armstrong';
}else{
echo 'its not armstrong';
}
}
?>
</body>
</html>
21. Write a program in PHP to find the factors of an input a number.
<html>
<body>
<form method="post">
<input type="number" name="num1">
<input type="submit" name="submit" value="find factors">
</form>
<?php
if(isset($_POST['submit'])){
$n=$_POST['num1'];
for($i=1;$i<=$n;$i++){
if($n%$i==0){
echo $i.' ';
}
}
}
?>
</form>
</body>
</html>
Comments
Post a Comment