0% found this document useful (0 votes)
5 views4 pages

PHP Assignment 1

The document contains a PHP assignment with ten programming tasks. These tasks include checking if a number is even or odd, determining if a year is a leap year, calculating the factorial of a number, summing the first 25 natural numbers, printing the Fibonacci series, swapping two numbers, checking for palindromes, counting digits, printing prime numbers, and reversing a number. Each task is accompanied by a sample PHP code implementation.
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)
5 views4 pages

PHP Assignment 1

The document contains a PHP assignment with ten programming tasks. These tasks include checking if a number is even or odd, determining if a year is a leap year, calculating the factorial of a number, summing the first 25 natural numbers, printing the Fibonacci series, swapping two numbers, checking for palindromes, counting digits, printing prime numbers, and reversing a number. Each task is accompanied by a sample PHP code implementation.
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/ 4

PHP ASSIGNMENT 1

1. Write a program to check if a given number is even or odd

<?php
$number = 7;
if ($number % 2 == 0) {
echo "The number $number is Even";
} else {
echo "The number $number is Odd";
}
?>

2. Write a program to check if a given year is leap year

<?php
$year = 2024;

if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {


echo "$year is a Leap Year";
} else {
echo "$year is not a Leap Year";
}
?>

3. Write a program to calculate the factorial of a number

<?php
$number = 5;

$factorial = 1;
for ($i = 1; $i <= $number; $i++) {
$factorial *= $i;
}

echo "The factorial of $number is: $factorial";


?>
4. Write a program to find the sum of first 25 natural numbers

<?php
$number = 25;
$sum = ($number * ($number + 1)) / 2;

echo "The sum of the first $number natural numbers is: $sum";
?>

5. Write a program to print the Fibonacci series upto 15 terms

<?php
$terms = 15;
$num1 = 0;
$num2 = 1;

echo "Fibonacci series up to $terms terms: ";


for ($i = 1; $i <= $terms; $i++) {
echo $num1 . " ";
$nextNum = $num1 + $num2;
$num1 = $num2;
$num2 = $nextNum;
}
?>

6. Write a program to swap two numbers without using a third variable

<?php
$num1 = 5;
$num2 = 10;
echo "Before swapping: num1 = $num1, num2 = $num2";

$num1 = $num1 + $num2;


$num2 = $num1 - $num2;
$num1 = $num1 - $num2;

echo "After swapping: num1 = $num1, num2 = $num2";


?>
7. Write a program to check if a number is a palindrome

<?php
$number = 121;
$originalNumber = $number;
$reversedNumber = 0;

while ($number > 0) {


$reversedNumber = ($reversedNumber * 10) + ($number % 10);
$number = floor($number / 10);
}

if ($originalNumber == $reversedNumber) {
echo "$originalNumber is a palindrome";
} else {
echo "$originalNumber is not a palindrome";
}
?>

8. Write a program to count the number of digits in a numbers

<?php
$number = 12345;
$digitCount = strlen((string)$number);

echo "The number $number has $digitCount digits";


?>

9. Write a program to print prime numbers from 1 to 100

<?php
echo "Prime numbers from 1 to 100: ";
for ($num = 2; $num <= 100; $num++) {
$isPrime = true;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
$isPrime = false;
break;
} }
if ($isPrime) {
echo $num . "\n";
}
?>
10. Write a program to reverse a number

<?php
$number = 12345;
$reversedNumber = 0;

while ($number > 0) {


$reversedNumber = ($reversedNumber * 10) + ($number % 10);
$number = floor($number / 10);
}

echo "The reversed number is: $reversedNumber";


?>

You might also like