0% found this document useful (0 votes)
6 views5 pages

Q 10

Computer Binary

Uploaded by

satwikdas124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Q 10

Computer Binary

Uploaded by

satwikdas124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

QUESTION 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:

•fill all the left diagonals with prime numbers only

•fill all the right diagonals with composite numbers only.

ALGORITHM:-

1. Start

2.Define DiagonalMatrix class

3.Input Validation:

*Read the order of the matrix n.

*Check if n is an even number. If not, prompt the user to enter an even number.

4.Matrix Initialization:

*Initialize an n x n matrix with zeros.

5.Prime Number Generation:

*Create a list to store prime numbers.

*Initialize a counter num starting from 2.

*While the size of the prime number list is less than n, check if num is prime.

*If num is prime, add it to the prime list.

*Increment num and repeat until we have n prime numbers.

6.Composite Number Generation:

*Create a list to store composite numbers.

*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).

*If num is composite, add it to the composite list.

*Increment num and repeat until we have n composite numbers.

7.Fill Diagonals In Matrix:

*Initialize two indices primeIndex and compositeIndex to 0.

*For each i from 0 to n-1:


*Assign the primeIndex-th prime number to matrix[i][i] (left diagonal).

*Increment primeIndex.

*Assign the compositeIndex-th composite number to matrix[i][n-1-i] (right diagonal).

*Increment compositeIndex.

8.Print:

*Print the matrix row by row.

9.End.

CODE:-

import java.util.ArrayList;

import java.util.Scanner;

public class DiagonalMatrix {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the order of the matrix (even number): ");

int n = scanner.nextInt();

if (n % 2 != 0) {

System.out.println("Please enter an even number.");

return;

int[][] matrix = new int[n][n];

ArrayList<Integer> primes = generatePrimes(n);

ArrayList<Integer> composites = generateComposites(n);


fillDiagonals(matrix, primes, composites);

System.out.println("Matrix with primes on left diagonals and composites on right diagonals:");

printMatrix(matrix);

private static void fillDiagonals(int[][] matrix, ArrayList<Integer> primes, ArrayList<Integer>


composites) {

int primeIndex = 0;

int compositeIndex = 0;

int n = matrix.length;

// Fill left diagonals with prime numbers

for (int i = 0; i < n; i++) {

matrix[i][i] = primes.get(primeIndex++);

// Fill right diagonals with composite numbers

for (int i = 0; i < n; i++) {

matrix[i][n - 1 - i] = composites.get(compositeIndex++);

private static ArrayList<Integer> generatePrimes(int limit) {

ArrayList<Integer> primes = new ArrayList<>();

int num = 2;

while (primes.size() < limit) {

if (isPrime(num)) {

primes.add(num);
}

num++;

return primes;

private static ArrayList<Integer> generateComposites(int limit) {

ArrayList<Integer> composites = new ArrayList<>();

int num = 4; // start from the first composite number

while (composites.size() < limit) {

if (!isPrime(num)) {

composites.add(num);

num++;

return composites;

private static boolean isPrime(int num) {

if (num <= 1) return false;

if (num == 2) return true;

if (num % 2 == 0) return false;

int maxDivisor = (int) Math.sqrt(num) + 1;

for (int i = 3; i < maxDivisor; i += 2) {

if (num % i == 0) return false;

return true;

}
private static void printMatrix(int[][] matrix) {

for (int[] row : matrix) {

for (int element : row) {

System.out.print(element + " ");

System.out.println();

Output:-

You might also like