Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Tutorial PHP and JS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

1. Write a PHP program to check whether a number is a prime number or not.

<?php
function isPrime($num) {
if ($num <= 1) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}

$number = 29; // Example number


echo isPrime($number) ? "$number is a prime number" : "$number is not a
prime number";
?>

2. Write a PHP program to check whether a number is palindrome or not.

<?php
function isPalindrome($num) {
return strval($num) === strrev(strval($num));
}

$number = 121; // Example number


echo isPalindrome($number) ? "$number is a palindrome": "$number is not a
palindrome";
?>

3. Write a PHP program to print the reverse of a number.

<?php

function reverseNumber($num) {

return strrev(strval($num));

$number = 12345; // Example number

echo "The reverse of $number is ". reverseNumber($number);

?>
4. List the different ways to create an array in PHP with an example.

<?php

$array1 = array(1, 2, 3);


$array2 = [4, 5, 6]; //
$array3 = array("first" => 1, "second" => 2);
$array4 = ["third" => 3, "fourth" => 4];

print_r($array1);
print_r($array2);
print_r($array3);
print_r($array4);
?>

5. Write a PHP program to compute the sum of the positive integers up to 100 using
do while.

<?php
$sum = 0;
$i = 1;
do {
$sum += $i;
$i++;
} while ($i <= 100);
echo "Sum using do-while: $sum";
?>

6. Write a PHP program to compute the sum of the positive integers up to 100 using
a 3 for loop.

<?php
$sum = 0;
for ($i = 1; $i <= 100; $i++) {
$sum += $i;
}
echo "Sum using for loop: $sum";
?>

7. Write a PHP program to compute the sum of the positive integers up to 100 using
a while loop.
<?php
$sum = 0;
$i = 1;
while ($i <= 100) {
$sum += $i;
$i++;
}
echo "Sum using while loop: $sum";
?>

8. Write a PHP program to print the sum of digits of a number.

<?php

function sumOfDigits($num) {

$sum = 0;

while ($num != 0) {

$sum += $num % 10;

$num = intval($num / 10);

return $sum;

$number = 123; // Example number

echo "Sum of digits of $number is " . sumOfDigits($number);

?>

9. Write a PHP program to check whether the given number is Armstrong or not.

<?php

function isArmstrong($num) {

$sum = 0;

$temp = $num;
while ($temp != 0) {

$digit = $temp % 10;

$sum += $digit ** 3;

$temp = intval($temp / 10);

return $sum === $num;

$number = 153; // Example number

echo isArmstrong($number) ? "$number is an Armstrong number" : "$number is


not an Armstrong number";

?>

10. Write a PHP code to display a message indicating whether the number is odd or

even while clicking on the submit button.

<!DOCTYPE html>

<html>

<body>

<form method="post">

Enter a number: <input type="number" name="number">

<input type="submit" value="Check">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$number = $_POST['number'];

echo ($number % 2 == 0) ? "$number is even" : "$number is odd";


}

?>

</body>

</html>

11. List out the sorting functions for Arrays in PHP. Illustrate with suitable examples.

<?php
$array = array(4, 2, 8, 6);
sort($array); // Ascending order sort
echo "Sorted in ascending order: ";
print_r($array);

rsort($array); // Descending order sort


echo "\nSorted in descending order: ";
print_r($array);
?>

12. Write a PHP program to find the factorial of a number.

<?php
function factorial($num) {
$fact = 1;
for ($i = 1; $i <= $num; $i++) {
$fact *= $i;
}
return $fact;
}

$number = 5; // Example number


echo "Factorial of $number is " . factorial($number);
?>

13. Describe how input from an HTML form is retrieved in a PHP program, with an
Example.

<!DOCTYPE html>
<html>
<body>
<form method="post">
Name: <input type="text" name="name">
<input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
echo "Hello, $name!";
}
?>
</body>
</html>

14. Design the HTML page which enters a given number and embeds the PHP
code to display a message indicating, whether the number is armstrong or not,
when clicking on the ‘CHECK NUMBER’ button.

<!DOCTYPE html>
<html>
<body>
<form method="post">
Enter a number: <input type="number" name="number">
<input type="submit" value="CHECK NUMBER">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
function isArmstrong($num) {
$sum = 0;
$temp = $num;
while ($temp != 0) {
$digit = $temp % 10;
$sum += $digit ** 3;
$temp = intval($temp / 10);
}
return $sum === $num;
}
echo isArmstrong($number) ? "$number is an Armstrong number" : "$number
is not an Armstrong number";
}
?>
</body>
</html>

JS

1. Write JavaScript code to print the following pattern.


1
01
101
0101
10101

for (let i = 1; i <= 5; i++) {


let row = "";
for (let j = 1; j <= i; j++) {
row += (i + j) % 2 + " ";
}
console.log(row);
}

2. Design a form using html and JavaScript to collect the details from the Car
manufacturing company such as Car name, Car model (Radio Button), Colour
and
Rate. Create a dynamic drop down list for colour and based on the colour,
another
drop down list contains a rate.

<!DOCTYPE html>
<html>
<body>
<form>
Car Name: <input type="text" name="carName"><br>
Car Model: <input type="radio" name="carModel" value="Model1">Model1
<input type="radio" name="carModel" value="Model2">Model2<br>
Colour:
<select id="color" onchange="updateRate()">
<option value="Red">Red</option>
<option value="Blue">Blue</option>
</select><br>
Rate:
<select id="rate">
<option value="5000">5000</option>
</select>
</form>
<script>
function updateRate() {
let color = document.getElementById("color").value;
let rate = document.getElementById("rate");
rate.innerHTML = color === "Red" ? "<option value='5000'>5000</option>" :
"<option value='6000'>6000</option>";
}
</script>
</body>
</html>

3. Write a JavaScript program to construct the following pattern, using a nested


for
loop.
*
**
***
****
*****

for (let i = 1; i <= 5; i++) {


let row = "";
for (let j = 1; j <= i; j++) {
row += "* ";
}
console.log(row);
}

4. Design a form using html and JavaScript to collect the details from the user
such
as name, gender (Radio Button), address, state, city. Create a dynamic drop
down list
for state and based on state another drop down list contains a list of cities for
state.
<!DOCTYPE html>
<html>
<body>
<form>
Name: <input type="text" name="name"><br>
Gender:
<input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female<br>
Address: <input type="text" name="address"><br>
State:
<select id="state" onchange="updateCity()">
<option value="State1">State1</option>
<option value="State2">State2</option>
</select><br>
City:
<select id="city">
<option value="City1">City1</option>
</select>
</form>
<script>
function updateCity() {
let state = document.getElementById("state").value;
let city = document.getElementById("city");
city.innerHTML = state === "State1" ? "<option value='City1'>City1</option>"
: "<option value='City2'>City2</option>";
}
</script>
</body>
</html>

5. Design a login form using HTML & JavaScript with following validations on
username and password fields.
1. Password length must be 6 to 12 characters.
2. Username should not start with _, @ or number.
3. Phone number must be 10 digits starting with 6/7/8/9.
4. All fields should not be blank.

<!DOCTYPE html>
<html>
<body>
<form onsubmit="return validateForm()">
Username: <input type="text" id="username"><br>
Password: <input type="password" id="password"><br>
Phone Number: <input type="text" id="phone"><br>
<input type="submit" value="Login">
</form>
<script>
function validateForm() {
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
let phone = document.getElementById("phone").value;
if (!/^[^\d_@][\w@]{5,11}$/.test(username)) {
alert("Invalid username.");
return false;
}
if (password.length < 6 || password.length > 12) {
alert("Password must be 6 to 12 characters.");
return false;
}
if (!/^[6-9]\d{9}$/.test(phone)) {
alert("Phone number must be 10 digits starting with 6, 7, 8, or 9.");
return false;
}
return true;
}
</script>
</body>
</html>

6. Write JavaScript code to know which mouse button was clicked,number of


elements in form, and write hello world on the document.

document.addEventListener('click', function(event) {
console.log("Mouse Button clicked:", event.button);
console.log("Number of elements in form:",
document.forms[0].elements.length);
document.body.innerHTML = "Hello World";
});

7. Show the use of JavaScript events for the following,


(i) Trap the exiting of the user from a page.
(ii) Show the heading. When the mouse is over the heading the background
colour
should be red and when the mouse goes out of the heading, colour should
change to
blue.

<h1 id="heading" onmouseover="this.style.backgroundColor='red'"


onmouseout="this.style.backgroundColor='blue'">Hover over me</h1>
<script>
window.onbeforeunload = function() {
return "Are you sure you want to leave?";
};
</script>

8. Write a JavaScript that handles following events.


(i) Gives the key code for the key pressed.
(ii) The script should give the message that "vowel is pressed" if vowel keys are
pressed.
(iii) Background colour should change to red after releasing the pressed key.

document.addEventListener('keydown', function(event) {
console.log("Key code:", event.keyCode);
if ('aeiou'.includes(event.key.toLowerCase())) {
alert("Vowel is pressed");
}
});
document.addEventListener('keyup', function() {
document.body.style.backgroundColor = 'red';
});

9. List the order of precedence of style levels. Design a sample web page for
providing ‘KTU BTech Honours Regulation 19’ for KTU and use embedded Style
sheet to apply minimum 5 styles for list, tables and pages.

<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial; background-color: #f0f0f0; }
ul { list-style-type: square; color: red; }
table { border-collapse: collapse; width: 50%; }
td, th { border: 1px solid black; padding: 8px; }
h1 { color: blue; }
</style>
</head>
<body>
<h1>KTU BTech Honours Regulation 19</h1>
<ul>
<li>Requirement 1</li>
<li>Requirement 2</li>
</ul>
<table>
<tr>
<th>Subject</th>
<th>Credits</th>
</tr>
<tr>
<td>Mathematics</td>
<td>4</td>
</tr>
</table>
</body>
</html>

You might also like