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

Php Index 18 Static Programs

The document contains various PHP code snippets demonstrating basic programming concepts such as arithmetic operations, area and volume calculations, number checks (odd/even, positive/negative, prime/composite), and control structures (loops, conditionals, switch case). Each snippet is designed to perform a specific task, such as calculating the sum of two numbers, checking for palindromes, or generating multiplication tables. The examples illustrate fundamental programming techniques useful for beginners.

Uploaded by

sudipbaniya0000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Php Index 18 Static Programs

The document contains various PHP code snippets demonstrating basic programming concepts such as arithmetic operations, area and volume calculations, number checks (odd/even, positive/negative, prime/composite), and control structures (loops, conditionals, switch case). Each snippet is designed to perform a specific task, such as calculating the sum of two numbers, checking for palindromes, or generating multiplication tables. The examples illustrate fundamental programming techniques useful for beginners.

Uploaded by

sudipbaniya0000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

sum of 2 numbers

<?php

$num1 = 10;

$num2 = 20;

$sum = $num1 + $num2;

echo "The sum of $num1 and $num2 is: $sum";

?>

2.area of the rectangle

<?php

$width = 10;

$height = 20;

$area = $width * $height;

echo "The area of the rectangle is: $area";

?>

3.area of a circle

<?php

$radius = 10;

$pi = 3.14159265;

$area = $pi * $radius * $radius;

echo "The area of the circle is: " . round($area, 2);

?>

4. volume of the sphere


<?php

$radius = 10;

$pi = 3.14159265;

$volume = (4/3) * $pi * $radius * $radius * $radius;

echo "The volume of the sphere is: " . round($volume, 2);

?>

5.check odd or even

<?php

$num = 10;

if ($num % 2 == 0) {

echo "$num is an even number.";

} else {

echo "$num is an odd number.";

?>

6. check whether positive negative or zero

<?php

$num = 10;

if ($num > 0) {

echo "$num is a positive number.";


} elseif ($num < 0) {

echo "$num is a negative number.";

} else {

echo "$num is zero.";

?>

7. greater among 2 numbers

<?php

$num1 = 10;

$num2 = 20;

if ($num1 > $num2) {

echo "$num1 is greater than $num2.";

} else {

echo "$num2 is greater than $num1.";

?>

8. smallest among 3 numbers

<?php

$num1 = 10;

$num2 = 20;

$num3 = 5;

if ($num1 <= $num2 && $num1 <= $num3) {


echo "$num1 is the smallest number.";

} elseif ($num2 <= $num1 && $num2 <= $num3) {

echo "$num2 is the smallest number.";

} else {

echo "$num3 is the smallest number.";

?>

9. check palindrome number or not

<?php

$num = 121;

$original_num = $num;

$reverse = 0;

while ($num > 0) {

$remainder = $num % 10;

$reverse = ($reverse * 10) + $remainder;

$num = (int)($num / 10);

if ($original_num == $reverse) {

echo "$original_num is a palindrome number.";

} else {

echo "$original_num is not a palindrome number.";

?>

10.check Armstrong number or not


<?php

$num = 153;

$original_num = $num;

$sum = 0;

$digits = 0;

while ($num > 0) {

$digits++;

$num = (int)($num / 10);

$num = $original_num;

while ($num > 0) {

$remainder = $num % 10;

$sum = $sum + pow($remainder, $digits);

$num = (int)($num / 10);

if ($original_num == $sum) {

echo "$original_num is an Armstrong number.";

} else {

echo "$original_num is not an Armstrong number.";

?>

11. check prime or composite


<?php

$num = 7;

$is_prime = true;

if ($num <= 1) {

$is_prime = false;

} else {

for ($i = 2; $i <= sqrt($num); $i++) {

if ($num % $i == 0) {

$is_prime = false;

break;

if ($is_prime) {

echo "$num is a prime number.";

} else {

echo "$num is a composite number.";

?>

12. to find the factors of a number

<?php

$num = 24;

echo "The factors of $num are: ";

for ($i = 1; $i <= $num; $i++) {

if ($num % $i == 0) {
echo "$i ";

?>

13. to find the factorial of a number

<?php

$num = 5;

$factorial = 1;

for ($i = 1; $i <= $num; $i++) {

$factorial = $factorial * $i;

echo "The factorial of $num is $factorial.";

?>

14. to find the multiplication table of a number

<?php

$num = 7;

echo "The multiplication table of $num is:<br>";

for ($i = 1; $i <= 10; $i++) {

echo "$num x $i = " . ($num * $i) . "<br>";

?>

15. sum of N natural numbers

<?php
$n = 10;

$sum = 0;

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

$sum = $sum + $i;

echo "The sum of first $n natural numbers is $sum.";

?>

16. to print 1 to 100 using for loop, while loop and do while loop

<?php

// Using while loop

echo "Using while loop:<br>";

$i = 1;

while ($i <= 100) {

echo $i . " ";

$i++;

// Using do...while loop

echo "<br><br>Using do...while loop:<br>";

$i = 1;

do {

echo $i . " ";

$i++;

} while ($i <= 100);

// Using for loop

echo "<br><br>Using for loop:<br>";


for ($i = 1; $i <= 100; $i++) {

echo $i . " ";

?>

17. to print the day of the week using switch case

<?php

$day = 4;

switch ($day) {

case 1:

echo "Monday";

break;

case 2:

echo "Tuesday";

break;

case 3:

echo "Wednesday";

break;

case 4:

echo "Thursday";

break;

case 5:

echo "Friday";

break;

case 6:

echo "Saturday";

break;
case 7:

echo "Sunday";

break;

default:

echo "Invalid day";

break;

?>

18. ternary operator

<?php

$number = 5;

$result = ($number > 0) ? "positive" : "negative";

echo "The number is " . $result;

?>

You might also like