w3resource

PHP Exercises: Check if the number of 3's is greater than the number of 5's

PHP Basic Algorithm: Exercise-113 with Solution

Write a PHP program to check if the number of 3's is greater than the number of 5's.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that takes an array of numbers as a parameter
function test($nums)
 { 
    // Initialize variables $no_3 and $no_5 with values of 0
    $no_3 = 0;
    $no_5 = 0;

    // Iterate through the elements of the array using a for loop
    for ($i = 0; $i < sizeof($nums); $i++)
    {
        // Check if the current element is equal to 3, increment $no_3 if true
        if ($nums[$i] == 3) $no_3++;
        
        // Check if the current element is equal to 5, increment $no_5 if true
        if ($nums[$i] == 5) $no_5++;
    }

    // Return true if the count of 3's is greater than the count of 5's, otherwise return false
    return $no_3 > $no_5;
 }   

// Use 'var_dump' to print the result of calling 'test' with different arrays
var_dump(test([1, 5, 6, 9, 3, 3]));
var_dump(test([1, 5, 5, 5, 10, 17]));
var_dump(test([1, 3, 3, 5, 5, 5]));
?>

Sample Output:

bool(true)
bool(false)
bool(false)

Flowchart:

Flowchart: Check if the number of 3's is greater than the number of 5's.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to check if the sum of all 5' in the array exactly 15 in a given array of integers.
Next: Write a PHP program to check if a given array of integers contains a 3 or a 5.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php-exercises/basic-algorithm/php-basic-algorithm-exercise-113.php