JAVA_CODINGS Telegram 73
53. Magic Matrix.

import java.io.*;

class MagicMatrix
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of the matrix : ");
int n = Integer.parseInt(br.readLine());

if (n > 5)
System.out.println("Enter a number between 1 to 5 ");
else
{
int A[][] = new int[n][n]; // Creating the Magic Matrix
int i, j, k, t;

/*Initializing every cell of the matrix with 0 */
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
A[i][j] = 0;
}
}

/* When the size of the matrix is Odd */
if (n % 2 != 0)
{
i = 0;
j = n / 2;
k = 1;
while (k <= n * n)
{
A[i][j] = k++;
i--; // Making one step upward
j++; // Moving one step to the right

if (i < 0 && j > n - 1) // Condition for the top-right corner element
{
i = i + 2;
j--;
}

if (i < 0) // Wrapping around the row if it goes out of boundary
i = n - 1;

if (j > n - 1) // Wrapping around the column if it goes out of boundary
j = 0;

if (A[i][j] > 0) // Condition when the cell is already filled
{
i = i + 2;
j--;
}
}
}
/* When the size of the matrix is even */
else
{
k = 1;

/* Filling the matrix with natural numbers from 1 till n*n */
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
A[i][j] = k++;
}
}

j = n - 1;

for (i = 0; i < n / 2; i++)
{
/* swapping corner elements of primary diagonal */
t = A[i][i];
A[i][i] = A[j][j];
A[j][j] = t;

/* swapping corner elements of secondary diagonal */
t = A[i][j];
A[i][j] = A[j][i];
A[j][i] = t;

j--;
}
}

/* Printing the Magic matrix */
System.out.println("The Magic Matrix of size " + n + "x" + n + " is:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
}
}
}

@java_codings



tgoop.com/java_codings/73
Create:
Last Update:

53. Magic Matrix.

import java.io.*;

class MagicMatrix
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the size of the matrix : ");
int n = Integer.parseInt(br.readLine());

if (n > 5)
System.out.println("Enter a number between 1 to 5 ");
else
{
int A[][] = new int[n][n]; // Creating the Magic Matrix
int i, j, k, t;

/*Initializing every cell of the matrix with 0 */
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
A[i][j] = 0;
}
}

/* When the size of the matrix is Odd */
if (n % 2 != 0)
{
i = 0;
j = n / 2;
k = 1;
while (k <= n * n)
{
A[i][j] = k++;
i--; // Making one step upward
j++; // Moving one step to the right

if (i < 0 && j > n - 1) // Condition for the top-right corner element
{
i = i + 2;
j--;
}

if (i < 0) // Wrapping around the row if it goes out of boundary
i = n - 1;

if (j > n - 1) // Wrapping around the column if it goes out of boundary
j = 0;

if (A[i][j] > 0) // Condition when the cell is already filled
{
i = i + 2;
j--;
}
}
}
/* When the size of the matrix is even */
else
{
k = 1;

/* Filling the matrix with natural numbers from 1 till n*n */
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
A[i][j] = k++;
}
}

j = n - 1;

for (i = 0; i < n / 2; i++)
{
/* swapping corner elements of primary diagonal */
t = A[i][i];
A[i][i] = A[j][j];
A[j][j] = t;

/* swapping corner elements of secondary diagonal */
t = A[i][j];
A[i][j] = A[j][i];
A[j][i] = t;

j--;
}
}

/* Printing the Magic matrix */
System.out.println("The Magic Matrix of size " + n + "x" + n + " is:");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
}
}
}

@java_codings

BY Advance Java πŸ‘¨β€πŸ’»


Share with your friend now:
tgoop.com/java_codings/73

View MORE
Open in Telegram


Telegram News

Date: |

Telegram Channels requirements & features Those being doxxed include outgoing Chief Executive Carrie Lam Cheng Yuet-ngor, Chung and police assistant commissioner Joe Chan Tung, who heads police's cyber security and technology crime bureau. How to create a business channel on Telegram? (Tutorial) Earlier, crypto enthusiasts had created a self-described β€œmeme app” dubbed β€œgm” app wherein users would greet each other with β€œgm” or β€œgood morning” messages. However, in September 2021, the gm app was down after a hacker reportedly gained access to the user data. β€œHey degen, are you stressed? Just let it all out,” he wrote, along with a link to join the group.
from us


Telegram Advance Java πŸ‘¨β€πŸ’»
FROM American