Programming Examples

Java program for wondrous square


A wondrous square is an n by n grid which fulfils the following conditions:

It contains integers from 1 to n2, where each integer appears only once.

The sum of integers in any row or column must add up to 0.5 x n x (n2 + 1).

For example, the following grid is a wondrous square where the sum of each row or column is 65 when n=5.

17241815
23571416
46132022
101219213
11182529

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.

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. For example, 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:

Input:

n = 4

161512
641014
98125
371113

Output:

Yes, it represents a wondrous square

PrimeRow IndexColumn Index
203
330
523
731
1132
1333
1501

Input:

n=3

124
375
896

Output

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz