There is a magic square matrix in such a way that sum of a
column or a row are same like

3 5 2
4 3 3
3 2 5
sum of each column and row is 10.

you have to check that matrix is magic matrix or not?



There is a magic square matrix in such a way that sum of a column or a row are same like 3 ..

Answer / hussein

public class MagicSquares {
public static void main(String[] args) {
int magic[][] = { { 3, 5, 2}, { 4, 3, 3}, { 3, 2, 5};
int total = 0, sum, sum1;
for (int r = 0; r < 3; ++r) {
for (int c = 0; c < 3; ++c) {
total += magic[r][c];
if (magic[r][c] < 10)
System.out.print(" " + magic[r][c]);
else
System.out.print(" " + magic[r][c]);
}
System.out.println();
}

int sumrow[] = new int[4];
for (int c = 0; c < 4; ++c)
sumrow[c] = 0;
for (int row = 0; row < 4; row++)
for (int c = 0; c < 4; ++c)
sumrow[row] += magic[row][c];

int sumcol[] = new int[4];
for (int c = 0; c < 4; ++c)
sumcol[c] = 0;
for (int col = 0; col < 4; col++)
for (int r = 0; r < 4; ++r)
sumcol[col] += magic[r][col];

int sumdiag[] = new int[2];
for (int diag = 0; diag < 4; diag++)
sumdiag[0] += magic[diag][diag];
for (int diag = 0; diag < 4; ++diag)
sumdiag[1] += magic[3 - diag][diag];

sum = total / 4;
sum1 = (sum + ((sumdiag[0] + sumdiag[1]) / 2)) / 2;

if (sum1 == sum)
System.out.println("This is a magic square!");
else
System.out.println("This is not a magic square!");

}

Is This Answer Correct ?    7 Yes 0 No

Post New Answer

More C++ General Interview Questions

Explain public, protected, private in c++?

0 Answers  


When is dynamic checking necessary?

0 Answers  


Why is swift so fast?

0 Answers  


write a program that reads in a file and counts the number of lines, words, and characters. Your program should ask the user to input a filename. Open the file and report an error if the file does not exist or cannot be opened for some other reason. Then read in the contents of the file and count the number of lines, words, and characters in the file. Also print additional information about the file, such as the longest and shortest words, and longest and shortest lines. For simplicity, we define a word to be one or more characters ending with white space (a space, tab, carriage return, etc.). Functions for checking the types of characters can be found in the ctype.h header file, so you want to include this header file in your program. For example, the sentence below could be all that is in a file. This sentence IT 104 is taught in C++. has 32 characters, one line, and six words. The shortest line is 32 characters. The longest line is 32 characters. The shortest word is 2 characters. The longest word is 6 characters

0 Answers  


what is the difference between linear list linked representaion and linked representation? what is the purpose of representing the linear list in linked represention ? is it not avoiding rules of linear represention?

0 Answers  






What do you mean by a template?

0 Answers  


What is nested class in c++?

0 Answers  


What is c++ array?

0 Answers  


Explain terminate() function?

0 Answers  


Why is c++ called oops?

0 Answers  


By using c++ with an example describe linked list?

0 Answers  


Why Pointers are not used in C++?

0 Answers   Global Logic,


Categories