Question: Write a program in JAVA to read n (2<=n<=10) and the value stored in the n by n cells and output if the grid represents a wondrous square. Also, output all prime numbers in the grid along with the row index and column index.
In this post, I will share the wondrous square program code with you. Firstly let's see what wondrous square is:
Wondrous Square:
A wondrous square is an n by n grid which fulfills the following two conditions:
(i) It contains integers from 1 to n2, where each integer appears only once.
(ii) The sum of integers in any row or column must add up to 0.5 * n * ( n2 + 1).
For example, The following grid is a wondrous square where the sum of each row or column is 65 when n = 5 :
17 24 2 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Sample Input and Output:
Input 1:
Enter the size of Wondrous Square n*n:
4
Enter Elements Row-Wise
16
15
1
2
6
4
10
14
9
8
12
5
3
7
11
13
Output 1:
16 15 1 2
6 4 10 14
9 8 12 5
3 7 11 13
Prime Row Index Column Index
2 0 3
3 3 0
5 2 3
7 3 1
11 3 2
13 3 3
Yes, it represents a wondrous square.
Input 2:
Enter the size of Wondrous Square n*n:2
Enter Elements Row-Wise
2
3
3
2
Output 2:
2 3
3 2
Prime Row Index Column Index
2 0 0
2 1 1
3 0 1
3 1 0
Not a Wondrous Square
Code :
If you have any doubts/questions related to Program, or if you want to give any suggestions, or want an explanation of code, feel free to comment below. I hope that my code is understandable. Please Subscribe to Programming Chaska for the latest programming updates and also to inspire me to write such unique and different programs.
Thank You for Visiting 😄
If you have any doubts/questions related to Program, or if you want to give any suggestions, or want an explanation of code, feel free to comment below. I hope that my code is understandable. Please Subscribe to Programming Chaska for the latest programming updates and also to inspire me to write such unique and different programs.
Thank You for Visiting 😄
Thank You for Visiting 😄
0 Comments