Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Magic square java user input

1 |import java.util.Scanner;
2 |public class MagicSquare3 {
3 |  public static void main(String[] args) { 
4 |	int i, j;
5 |	int sum_row, sum_col, sum_diagonal = 0, sum = 0;
6 |	boolean magic=true;
7 |	int[][] square = new int[3][3];
8 |	Scanner input = new Scanner(System.in);
9 |
10|	//Read number for each cell
11|	System.out.print("Enterers --> ");
12|	for (i=0; i<3; i++)
13|	   for (j=0; j<3; j++) 
14|	      square[i][j] = input.nextInt();
15|
16|	//Display square
17|	System.out.println("Square;
18|	for (i=0; i<3; i++) {
19|	   for (j=0; j<3; j++) 
20|	      System.out.print(square[i][j] + " ");
21|	   System.out.println();
22|	}
23|
24|	//Calculate sum of the first row
25|	for (j=0; j<3; j++)
26|	   sum += square[0][j];
27|
28|	//Calculate sum of 2nd and 3rd row
29|	for (i=1; i<3; i++) {
30|	   sum_row = 0;
31|	   for (j=0; j<3; j++)
32|	      sum_row += square[i][j];
33|	   if (sum_row != sum) {
34|	      magic = false;
35|	      break;
36|	   }
37|	}
38|	
39|	//Calculate sum of each column
40|	if (magic) {
41|	   for (j=0; j<3; j++) {
42|	      sum_col = 0;
43|	      for (i=0; i<3; i++)
44|		 sum_col += square[i][j];
45|	      if (sum_col != sum) {
46|		 magic = false;
47|		 break;
48|	      }
49|	   }
50|	}
51|	
52|	//Calculate sum of first diagonal
53|	if (magic) {
54|	   for (i=0; i<3; i++)
55|	      for (j=0; j<3; j++)
56|	         if (i==j)
57|		    sum_diagonal += square[i][j];
58|	   if (sum_diagonal != sum) {
59|	      magic = false;
60|	   }
61|	}
62|
63|	//Calculate sum of second diagonal
64|	if (magic) {
65|	   sum_diagonal = 0;
66|	   for (i=0; i<3; i++)
67|	      for (j=0; j<3; j++)
68|		 if ((i+j) == 2)
69|		    sum_diagonal += square[i][j];
70|	   if (sum_diagonal != sum) {
71|	      magic = false;
72|	   }
73|	}
74|	
75|	//Display result
76|	if (magic)
77|	   System.out.println("It magic square!");
78|	else
79|	   System.out.println("ItOT a magic square.");
80|  } 
81|}
Comment

PREVIOUS NEXT
Code Example
Java :: enum to get status name from list using status id 
Java :: Java Shuffling Using shuffle() 
Java :: java.lang.noclassdeffounderror even though class is present 
Java :: tipo map que permite armazenar na mesma chave java 
Java :: Add Future Date in AndroidStudio 
Java :: implement hashmap 
Java :: Retries Java 
Java :: how to count an replace string in java 
Java :: tipe data c++ 
Java :: java load config file 
Java :: find min max and average arraylist 
Java :: Write a java program to print a number from the user 
Java :: set default messaging app android manifest 
Java :: change focus of edittext android when click outside 
Java :: ggt euklidischer algorithmus java 
Java :: IMEI DEVISE ANDROID JAVA 
Java :: Calling the Pre-Defined Method in Java 
Java :: randpm years java 
Java :: ClassCastException Casting toArray() method 
Java :: how to check if rs next is null 
Java :: How tomake teris in Java 
Java :: add two numbers in java 
Java :: connect 2 package in android 
Java :: material design implement full screen dialog android java 
Java :: add SOSL to apex Example 
Java :: stack overflow recyclerview 
Java :: arrotondare un numero a 2 cifre dopo la virgola java 
Java :: save documents on Android 11 site:stackoverflow.com 
Java :: Clicking on Fragment goes through in Activity 
Java :: how to send images to storage using Admin Sdk in java 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =