-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMagic_Square_Agorithm_Project.java
More file actions
84 lines (72 loc) · 2.85 KB
/
Magic_Square_Agorithm_Project.java
File metadata and controls
84 lines (72 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Magic_Square_Agorithm_Project.
* This Algorithm is to help, given an odd number from 3 to 25, to arrange the numbers from 1 to the square
* of the number chose in a way that the addition of all the numbers in a row, column, left diagonal
* and right diagonal are equal.
* Hismark Carrera
* Fall-2017 Discrete Mathematics Assignment
*/
import java.util.Scanner;
public class Magic_Square_Agorithm_Project
{
public static void main(String[] args) {
System.out.println();
Scanner input = new Scanner(System.in);
System.out.print("Please enter an odd number from 3 upto 25: ");
int n = input.nextInt();
while(n % 2 == 0){
System.out.println("The number you entered is not an odd number: ");
System.out.println("Please enter an odd number: ");
n = input.nextInt();
}
System.out.println("Your Magic Square will be: " + n + " by " + n);
System.out.println("The total numbers to be arrange are: " + n*n);
System.out.println("The Magic Number is: " + ( n * ((n * n +1)/2)));
System.out.println();
int magic [][] = new int[n][n];
int row = 0;
int col = n/2;
magic[row][col] = 1;
for (int i = 2; i <= n*n; i++) {
if (magic[(row + n -1) % n][(col + 1) % n] == 0) {
row = (row +n - 1) % n;
col = (col + 1) % n;
}
else {
row = (row + 1) % n;
// don't change col
}
magic[row][col] = i;
}
// print results
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (magic[i][j] < 10) System.out.print("| "); // for alignment
if ((magic[i][j] >= 10) && (magic[i][j] < 100)) System.out.print("| "); // for alignment
if ((magic[i][j] >= 100) && (magic[i][j] < 1000)) System.out.print("|");
System.out.print(magic[i][j] + " ");
}
System.out.println();
}
System.out.println();
// calling the function to print the sum of the diagonals
System.out.println("The sum of main Diagonal from left(top) to right(bottom) is : " + diag1(magic));
System.out.println("The sum of the other main Diagonal from right(top) to left(bottom) is: " + diag2(magic));
}
public static int diag1(int Z[][])
{ // function to do the sum of the first diagonal
int size = Z[0].length;
int s = 0;
for ( int i =0; i < size; i++)
s+= Z[i][i];
return s;
}
public static int diag2(int Y[][])
{ // function to do the second diagonal
int size = Y[0].length;
int s = 0;
for ( int i =0; i < size; i++)
s+= Y[i][size-1];
return s;
}
}