Ticker

6/recent/ticker-posts

Wondrous Square Program in JAVA

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 :



// Write a program in JAVA to read n(2<=n<=10) and the value stored in these n by n cells and output if grid represents a wondrous square .Also output all prime numbers in the grid along with row index and column index.
import java.io.*;

public class Wondrous_Square {
    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int ijkt1 = 0t2 = 1t = 0t3 = 0;// Initialising variables
        System.out.print("Enter size of Wondrous Square n*n");
        int n = Integer.parseInt(in.readLine());// Accepting and Converting String To Integer
        int a[][] = new int[n][n];// array to store elements by user
        System.out.println("Enter Elements Row-Wise");
        for (i = 0i < ni++)
            for (j = 0j < nj++)
                a[i][j] = Integer.parseInt(in.readLine());// Accepting elements of array
        for (i = 0i < ni++) {
            for (j = 0j < nj++)
                System.out.print(a[i][j] + "\t");// Displaying elements of array
            System.out.println();
        }
        System.out.println("Prime\tRow Index\tColumn Index");
        for (k = 1k <= n * nk++)// loop from 1 to square of 'n'
        {
            t1 = 0;
            for (i = 0i < ni++) {
                int rs = 0cs = 0;
                for (j = 0j < nj++) {
                    if (k == a[i][j]) {
                        t1 = t1 + 1;
                        for (int l = 1l <= kl++)// loop for checking prime number
                            if (k % l == 0)
                                t3 = t3 + 1;
                        if (t3 == 2)
                            System.out.println(a[i][j] + "\t" + i + "\t\t" + j);// printing prime number
                        t3 = 0;
                    }
                    rs = rs + a[i][j];// storing sum of each element in a row
                    cs = cs + a[j][i];// storing sum of each element in a column
                    if (j == (n - 1) && rs != 0.5 * n * ((n * n) + 1) && cs != 0.5 * n * ((n * n) + 1))
                        t2 = 0;
                }
            }
            if (t1 == 1 && t2 == 1)
                t = 1;
        }
        if (t == 0)
            System.out.println("Not a Wondrous Square");
        else
            System.out.println("Yes,it represents a wondrous square");
    }
}

Output Window:


Output
Output Window


If you have any doubts/questions related to Program, or if you want to give any suggestions, or want an explanation of codefeel 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 😄

Post a Comment

0 Comments