C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to find the frequency of odd & even numbers in the given MatrixExplanationIn this program, we need to find the frequencies of odd and even numbers present in the matrix. In the above example, all odd numbers are represented by the blue square and even numbers are represented by red circles. To find the frequencies of odd and even numbers, loop through the array and check if the element of the array is divisible by 2. If it is divisible by 2(even) then, increment the count of countEven by 1. Else, increment the countOdd by 1. Algorithm
SolutionPython#Initialize matrix a a = [ [4, 1, 3], [3, 5, 7], [8, 2, 6] ]; countOdd = 0; countEven = 0; #Calculates number of rows and columns present in given matrix rows = len(a); cols = len(a[0]); #Counts the number of even elements and odd elements for i in range(0, rows): for j in range(0, cols): if(a[i][j] % 2 == 0): countEven = countEven + 1; else: countOdd = countOdd + 1; print("Frequency of odd numbers: " + str(countOdd)); print("Frequency of even numbers: " + str(countEven)); Output: Frequency of odd numbers: 5 Frequency of even numbers: 4 C#include <stdio.h> int main() { int rows, cols, countOdd = 0, countEven = 0; //Initialize matrix a int a[][3] = { {4, 1, 3}, {3, 5, 7}, {8, 2, 6} }; //Calculates number of rows and columns present in given matrix rows = (sizeof(a)/sizeof(a[0])); cols = (sizeof(a)/sizeof(a[0][0]))/rows; //Counts the number of even elements and odd elements for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(a[i][j] % 2 == 0) countEven++; else countOdd++; } } printf("Frequency of odd numbers: %d\n", countOdd); printf("Frequency of even numbers: %d\n", countEven); return 0; } Output: Frequency of odd numbers: 5 Frequency of even numbers: 4 JAVApublic class OddEven { public static void main(String[] args) { int rows, cols, countOdd = 0, countEven = 0; //Initialize matrix a int a[][] = { {4, 1, 3}, {3, 5, 7}, {8, 2, 6} }; //Calculates number of rows and columns present in given matrix rows = a.length; cols = a[0].length; //Counts the number of even elements and odd elements for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(a[i][j] % 2 == 0) countEven++; else countOdd++; } } System.out.println("Frequency of odd numbers: " + countOdd); System.out.println("Frequency of even numbers: " + countEven); } } Output: Frequency of odd numbers: 5 Frequency of even numbers: 4 C#using System; public class OddEven { public static void Main() { int rows, cols, countOdd = 0, countEven = 0; //Initialize matrix a int[,] a = { {4, 1, 3}, {3, 5, 9}, {8, 2, 6} }; //Calculates number of rows and columns present in given matrix rows = a.GetLength(0); cols = a.GetLength(1); //Counts the number of even elements and odd elements for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j++){ if(a[i,j] % 2 == 0) countEven++; else countOdd++; } } Console.WriteLine("Frequency of odd numbers: " + countOdd); Console.WriteLine("Frequency of even numbers: " + countEven); } } Output: Frequency of odd numbers: 5 Frequency of even numbers: 4 PHP<!DOCTYPE html> <html> <body> <?php //Initialize matrix a $a = array( array(4, 1, 3), array(3, 5, 7), array(8, 2, 6) ); $countEven = 0; $countOdd = 0; //Calculates number of rows and columns present in given matrix $rows = count($a); $cols = count($a[0]); //Counts the number of even elements and odd elements for($i = 0; $i < $rows; $i++){ for($j = 0; $j < $cols; $j++){ if($a[$i][$j] % 2 == 0) $countEven++; else $countOdd++; } } print("Frequency of odd numbers: " . $countOdd); print("<br>Frequency of even numbers: " . $countEven); ?> </body> </html> Output: Frequency of odd numbers: 5 Frequency of even numbers: 4
Next Topic#
|