Thursday, July 4, 2024
HomeLanguagesPhpPhp Program for Frequencies of even and odd numbers in a matrix

Php Program for Frequencies of even and odd numbers in a matrix

Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix 
Examples: 
 

Input : m = 3, n = 3
        { 1, 2, 3 }, 
        { 4, 5, 6 }, 
        { 7, 8, 9 }
Output : Frequency of odd number =  5 
         Frequency of even number = 4


Input :   m = 3, n = 3
         { 10, 11, 12 },
         { 13, 14, 15 },
         { 16, 17, 18 }
Output : Frequency of odd number  =  4 
         Frequency of even number  = 5

 

PHP




<?php
// PHP Program to Find the frequency
// of even and odd numbers in a matrix
$MAX = 100;
 
// function for calculating frequency
function freq($ar, $m, $n)
{
    $even = 0; $odd = 0;
     
    for($i = 0; $i < $m; ++$i)
    {
        for ( $j = 0; $j < $n; ++$j)
        {
            // modulo by 2 to check
            // even and odd
            if (($ar[$i][$j] % 2) == 0)
                ++$even;
            else
                ++$odd;
        }
    }
     
    // print Frequency of numbers
    echo " Frequency of odd number = "
                           , $odd,"
";
    echo " Frequency of even number = "
                               , $even;
}
 
    // Driver code
    $m = 3; $n = 3;
    $array = array(array(1, 2, 3),
                   array(4, 5, 6),
                   array(7, 8, 9));
    freq($array, $m, $n);
 
// This code is contributed by anuj_67.
?>


Output: 
 

 Frequency of odd number = 5  
 Frequency of even number = 4

Time Complexity: O(m*n)

Auxiliary Space: O(1)

Please refer complete article on Frequencies of even and odd numbers in a matrix for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments