Q 10
Q 10
Write a program to accept the elements in a matrix of order n×n , where n is a even number and
performs the following task:
ALGORITHM:-
1. Start
3.Input Validation:
*Check if n is an even number. If not, prompt the user to enter an even number.
4.Matrix Initialization:
*While the size of the prime number list is less than n, check if num is prime.
*Initialize a counter num starting from 4 (since 4 is the first composite number).
*While the size of the composite number list is less than n, check if num is composite (i.e., not prime).
*Increment primeIndex.
*Increment compositeIndex.
8.Print:
9.End.
CODE:-
import java.util.ArrayList;
import java.util.Scanner;
int n = scanner.nextInt();
if (n % 2 != 0) {
return;
printMatrix(matrix);
int primeIndex = 0;
int compositeIndex = 0;
int n = matrix.length;
matrix[i][i] = primes.get(primeIndex++);
matrix[i][n - 1 - i] = composites.get(compositeIndex++);
int num = 2;
if (isPrime(num)) {
primes.add(num);
}
num++;
return primes;
if (!isPrime(num)) {
composites.add(num);
num++;
return composites;
return true;
}
private static void printMatrix(int[][] matrix) {
System.out.println();
Output:-