Q 14
Q 14
ALGORITHM
-______________________________________________________-
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[]
-______________________________________________________-
-______________________________________________________-