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

php pr3_2

The document contains several PHP code snippets demonstrating basic programming concepts. It includes examples for calculating the sum of even numbers, summing the digits of a number, converting decimal to binary, and printing number and star patterns. Each code snippet is followed by its expected output.

Uploaded by

thopteayush604
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)
4 views

php pr3_2

The document contains several PHP code snippets demonstrating basic programming concepts. It includes examples for calculating the sum of even numbers, summing the digits of a number, converting decimal to binary, and printing number and star patterns. Each code snippet is followed by its expected output.

Uploaded by

thopteayush604
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/ 2

1.

Find sum of all even numbers between 1 to 10


<?php
$n = 10; $sum =
0;

for ($i = 2; $i <= $n; $i += 2)

$sum += $i;

echo "Sum of even numbers from 1 to $n: $sum"; ?>

Output:
Sum of even numbers from 1 to 10: 30

2. Calculate Sum of Digits of a number


<?php
$number = 1234; $sum =
0;

while ($number > 0)

$sum += $number % 10;

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

echo "Sum of digits: $sum"; ?>

Output:
Sum of digits: 10

3. Convert Decimal to Binary number system


<?php
$decimal = 10;

$binary = decbin($decimal);

echo "Binary of $decimal: $binary";


?>
Output:
Binary of 10: 1010
4. Print Number Pattern
<?php for ($i = 1; $i <= 4; $i++) {
for ($j = 1; $j <= $i; $j++)
{
echo $j;
}
echo "\n";
}
?>

Output:
1
12
123
1234

5. Print Star Pattern


<?php
for ($i = 1; $i <= 5; $i++)
{
for ($j = 1; $j <= $i; $j++)
{
echo "* ";
}
echo "\n";
}
for ($i = 4; $i >= 1; $i--)
{
for ($j = 1; $j <= $i; $j++)
{
echo "* ";
}
echo "\n";
}
?>

Output:
*
* *
* **
* ***
* ****
* ***
* **
* *
*

You might also like