You are on page 1of 6

QUESTION

A wondrous square is an n by n grid which fulfils the following 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 * (n 2 + 1).
For example the following grid is a wondrous square where the sum of each
row or column is 65 when n = 5 :
17
23
4
10
11

24
5
6
12
18

2
7
13
19
25

8
14
20
21
2

15
16
22
3
9

Write a program to read n (2 < = n < = 10) and the values stored in these n by n
cells and output if the grid represents a wondrous square or not.
Also output all the prime numbers in the grid along with their row index and
column index as shown in the output. A natural number is said to be prime if it
has exactly two divisors. E.g. 2, 3, 5, 7, 11,.......
The first element of the given grid i.e. 17 is stored at row index 0 and column
index 0 and the next element in the row i.e. 24 is stored at row index 0 and
column index 1.
Test your program for the following data and some random data.
SAMPLE DATA:
INPUT: N = 4
16
6
9
3

15
4
8
7

1
10
12
11

OUTPUT:
YES IT REPRESENTS A WONDROUS SQUARE.
PRIME
ROW INDEX
2
0
3
3
5
2
7
3
11
3
13
3

COLUMN INDEX
3
0
3
1
2
3

ALGORITHM
-______________________________________________________-

2
14
5
13

Step-1-Start
Step-2-A class WondrousSquare is created
Step-3-Variables arr[][],arr1[],n,I,j,x,r,c,flag are created
Step-4-A function take() is created
Step-5-display "\nEnter the size of array(row and column same):"
Step-6-store n
Step-7- store arr[ ][ ] size as n*n
Step-8-store arr1[ ] size as 2*n
Step-9- for(i=0 to n;i++){
Step-10-for(j=0 to n;j++){
Step-11-display "\nEnter the value:"
Step-12-store arr[i][j] } }
Step-13-display "\nThe matrix is\n"
Step-14-for(i=0 to i< n;i++) {
Step-15-Store r=0;
Step-16-Store c=0;
Step-17-for(j=0 to n;j++) {
Step-18-display arr[i][j]+" "
Step-19-store r=r+arr[i][j]
Step-20-store c=c+arr[j][i] }
Step-21-store arr1[x]=r
Step-22-store arr1[x+n-1]=c
Step-23-increment x}
Step-24-for(i=0 to x;i++) {
Step-25-check if(arr1[i]!= 0.5 * n * (n*n + 1))
Step-26-if true then terminate from loop }
Step-27-if(i==x)
Step-28-display"YES IT REPRESENTS A WONDROUS SQUARE."
Step-29-display "IT IS NOT A WONDROUS SQUARE."
Step-30-display "PRIME ROW COLUMN"
Step-31-for(i=0 to n;i++){
Step-32-display for(j=0 to n;j++){
Step-33-check if(prime(arr[i][j]))
Step-34-display arr[i][j]+ " "+i+ " "+j }}
Step-35-end of take() function
Step-36-A function prime (int no) of boolean returntype is created
Step-37-A variable index is created
Step-38-for(index=2 to no;index++) {
Step-39-check if(no%index==0)
Step-40 terminate from loop}
Step-41-check if(index==no)
Step-42-if true then return true
Step-43-if false then return false
Step-44-end of prime() function
Step-45-A main() function is created
Step-46-An object ob of class WondrousSquare is created
Step-47-call take() using object ob
Step-48-end of main()
Step-49-Stop

-______________________________________________________-

PROGRAM
import java.io.*;
class WondrousSquare
{
int arr[][],arr1[];;
int n,i,j,x=0,r,c;
int flag;
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
public void take()throws Exception
{
System.out.println("\nEnter the size of array(row and column same):");
n=Integer.parseInt(stdin.readLine().trim());
arr=new int[n][n];
arr1=new int[2*n];
for(i=0;i< n;i++){
for(j=0;j< n;j++){
System.out.println("\nEnter the value:");
arr[i][j]=Integer.parseInt(stdin.readLine());
}
}
System.out.println("\nThe matrix is\n");
for(i=0;i< n;i++)
{
r=0;
c=0;
for(j=0;j< n;j++)
{
System.out.print(arr[i][j]+" ");
r=r+arr[i][j];
c=c+arr[j][i];
}
System.out.println();
arr1[x]=r;
arr1[x+n-1]=c;
x++;
}
for(i=0;i< x;i++)
{
if(arr1[i]!= 0.5 * n * (n*n + 1))
break;
}
if(i==x)
System.out.println("YES IT REPRESENTS A WONDROUS SQUARE.");
else
System.out.println("IT IS NOT A WONDROUS SQUARE.");
System.out.println("PRIME ROW COLUMN");
for(i=0;i< n;i++){
-______________________________________________________-

for(j=0;j< n;j++){
if(prime(arr[i][j]))
System.out.println(arr[i][j]+ " "+i+ " "+j);
}
}
}
private boolean prime(int no)
{
int index;
for(index=2;index< no;index++)
{
if(no%index==0)
break;}
if(index==no)
return true;
else
return false;
}
public static void main(String args[])throws Exception
{
WondrousSquare ob=new WondrousSquare();
ob.take();
}
}

-______________________________________________________-

VARIABLE DESCRIPTION
Data Type

Variable Name

Description

int
int
int
int
int
int

j
c
r
n
i
arr[]

int

arr1[]

Looping variable
column variable
row variable
To store size
Looping variable
An array to store the
values
An array of size double
than arr[]

-______________________________________________________-

INPUT AND OUTPUT


1.Input/Output
Enter the size of array(row and column same):
3
Enter the value:
12
Enter the value:
12
Enter the value:
32
Enter the value:
12
Enter the value:
23
Enter the value:
12
Enter the value:
23
Enter the value:
32
Enter the value:
12
The matrix is
12 12 32
12 23 12
23 32 12
IT IS NOT A WONDROUS SQUARE.
PRIME ROW COLUMN
23 1 1
23 2 0

-______________________________________________________-

You might also like