C Online Compiler
Example: Find Odd/Even Frequency in Matrix in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Odd/Even Frequency in Matrix #include <stdio.h> int main() { int rows, cols; // Step 1: Get matrix dimensions from the user printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &cols); int matrix[rows][cols]; // Declare matrix based on user input int oddCount = 0; int evenCount = 0; // Step 2: Get matrix elements from the user printf("Enter elements of the matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("Enter element at matrix[%d][%d]: ", i, j); scanf("%d", &matrix[i][j]); } } // Step 3: Iterate through the matrix and count odd/even numbers for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] % 2 == 0) { evenCount++; // Increment even counter if remainder is 0 } else { oddCount++; // Increment odd counter otherwise } } } // Step 4: Display the results printf("\nMatrix entered:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%4d", matrix[i][j]); } printf("\n"); } printf("\nFrequency of Even Numbers: %d\n", evenCount); printf("Frequency of Odd Numbers: %d\n", oddCount); return 0; }
Output
Clear
ADVERTISEMENTS