Java code help - තේරෙන්නෙ නෑ බං..
මේක මං internet එකෙන් ගත්ත code එකක් බං. Solve 3x3 magic puzzele..
තේරෙන්නෙ නෑ බං මොනවද කරල තියෙන්නෙ. ඇයි එහෙම කරල තියෙන්නෙ කියල.
Code:
class Example{
public static void main(String args[]){
int A[][]=new int[3][3]; // Creating the Magic Matrix
int i=0,j=1,k=1;
while(k<=9){
A[i][j] = k++;
i--; // Making one step upward
j++; // Moving one step to the right
if(i<0 && j>2) // Condition for the top-right corner element
{
i = i+2;
j--;
}
if(i<0) // Wrapping around the row if it goes out of boundary
i = 2;
if(j>2) // Wrapping around the column if it goes out of boundary
j = 0;
if(A[i][j]>0) // Condition when the cell is already filled
{
i = i+2;
j--;
}
}
/* Printing the Magic matrix */
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
}
}
තේරෙන්නෙ නෑ බං මොනවද කරල තියෙන්නෙ. ඇයි එහෙම කරල තියෙන්නෙ කියල.
Last edited:


