Write a program in c to check whether given matrix is prime matrix?

1 answer

Answer

1225060

2026-06-06 09:25

+ Follow

A prime matrix is defined as a matrix where all its elements are prime numbers. To check if a given matrix is a prime matrix in C, you can iterate through each element of the matrix, check if each number is prime using a helper function, and return false if any number is not prime. Here's a simple implementation:

<code class="language-c">#include <stdio.h>

int isPrime(int num) { if (num <= 1) return 0; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return 0; } return 1; }

int isPrimeMatrix(int matrix[3][3], int rows, int cols) { for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) if (!isPrime(matrix[i][j])) return 0; return 1; }

int main() { int matrix[3][3] = {{2, 3, 5}, {7, 11, 13}, {17, 19, 23}}; printf(isPrimeMatrix(matrix, 3, 3) ? "Prime Matrix\n" : "Not a Prime Matrix\n"); return 0; }

</code>
ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.