0% found this document useful (0 votes)
42 views6 pages

Lab Assignment-181002013

This document contains the code solutions to 4 coding problems in PHP along with sample outputs. The problems include: 1) Writing a function to reverse digits of a number and calculating the reversed sum of arrays of numbers. 2) Finding the most frequent number in an array. 3) Generating an 8x8 chessboard with alternating black and white tiles. 4) Adding numbers from 1-99 except multiples of 11 to an array.

Uploaded by

Abu Talha
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)
42 views6 pages

Lab Assignment-181002013

This document contains the code solutions to 4 coding problems in PHP along with sample outputs. The problems include: 1) Writing a function to reverse digits of a number and calculating the reversed sum of arrays of numbers. 2) Finding the most frequent number in an array. 3) Generating an 8x8 chessboard with alternating black and white tiles. 4) Adding numbers from 1-99 except multiples of 11 to an array.

Uploaded by

Abu Talha
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/ 6

“Lab Assignment”

Name: Mahmuda Akter

ID: 181002013

Problem#01:

<?php

function reverse($num)

$revnum = 0;

while ($num > 1)

$rem = $num % 10;

$revnum = ($revnum * 10) + $rem;

$num = ($num / 10);

return $revnum;

$numbers = [123, 234, 12];

$reversed_sum = 0;

foreach ($numbers as $number) {


$reversed_sum += reverse($number);

print($reversed_sum);

?>

Code With Output:


Problem#02:

<?php

$numbers = [4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3];

$max_count = 0;

$max_count_number = 0;

$counter = [];

foreach ($numbers as $number)

$number_hash = "number_".$number;

if (isset($counter[$number_hash])) {

$counter[$number_hash] += 1;

} else {

$counter[$number_hash] = 1;

if ($counter[$number_hash] > $max_count) {

$max_count = $counter[$number_hash];

$max_count_number = $number;

print($max_count_number);

?>
Code With Output:

Problem#03:

<?php

$chess_board = [];

$current_color = 0; // 0 = white, 1 = black

for ($i = 0; $i<8; $i++) {

for ($j=0; $j<8; $j++) {

$chess_board[$i][$j] = (int) $current_color;

$current_color = !$current_color;

$current_color = !$current_color;

}
for ($i = 0; $i<8; $i++) {

for ($j=0; $j<8; $j++) {

print($chess_board[$i][$j] . " ");

print("\n");

?>

Code With Output:

Problem#04:

<?php

$numbers = [];

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

if ($i < 10) $i = "0".$i;


if ($i % 11 != 0) $numbers[] = $i;

print(implode(', ', $numbers));

?>

Code With Output:

You might also like