12,630,913 members (33,345 online)
Tip/Trick
Add your own
alternative version

13.9K views
3 bookmarked
Posted

# Magic square in VB.NET

, 14 Dec 2011 CPOL
 Rate this:
Please Sign up or sign in to vote.
Calculating the magic square in VB.NET.

This is a simple console application developed with VB.NET for gerating the magic square of an odd number.

The magic squares problem leads to work into areas of mathematics such as theories of groups, lattices, Latin squares, determinants, partitions, matrices, and congruence arithmetic. Computer scientists are also perplexed by the difficulty of generating all magic squares of larger sizes.

In the solution, `Sum` shows the summation of any row, column, or diagonal.

The complete code is like this....

```Module Module1
Public s, size As Integer

Sub Main()
Dim n As Integer = 1
Dim i As Integer = 1
Dim j As Integer = 2

Console.WriteLine()
Console.Write("ENTER THE SIZE OF MAGIC SQUARE:  ")
s = Console.ReadLine()
size = s
Dim sum As Integer = 0

Dim ans(s, s) As Integer

If s <> 3 Then
Dim tmp As Integer = (s - 3) \ 2
i = i + tmp
j = j + tmp
End If

While n <= s * s

If ans(i, j) = 0 Then
ans(i, j) = n
Else
n = n - 1
i = i + 2
End If

i = i - 1
j = j + 1

If (i = -1 And j = s) Then
i = 0
j = 1
End If

If j >= s Then
j = j - s
End If

If i < 0 Then
i = s - 1
End If
n = n + 1
End While

Console.WriteLine()
Console.WriteLine("MAGIC SQUARE:")
Console.WriteLine()

For k As Integer = 0 To size - 1
For l As Integer = 0 To size - 1
Console.Write("    " & ans(k, l))
Next
Console.WriteLine()
Next

Console.WriteLine()
sum = ans(s \ 2, s \ 2) * s
Console.Write("SUM: " & sum)

Console.ReadKey()
End Sub

End Module```

The summation is based on this equation:

`SUM =  { N ( N^2 + 1 ) } /  2`

Where the N is number of row or column. If N = 5, then the result will be 65.

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

 Software Developer India
No Biography provided

## Comments and Discussions

 First Prev Next
 Re: Okay so what does it mean Peter Hawke22-Dec-11 13:55 Peter Hawke 22-Dec-11 13:55
 Re: :) NikulDarji20-Dec-11 3:40 NikulDarji 20-Dec-11 3:40
 Re: I agree, that paying attention to the logic behind the progr... Member 841302020-Dec-11 2:44 Member 8413020 20-Dec-11 2:44
 I agree, that paying attention to the logic behind the program is important - and I imagine that the logic was in the forefront of your thoughts when you posted the article. In that context, my comment was essentially off-topic, and I apologise for not properly introducing my feedback. I'll strongly disagree, however, that attending to the variables' names is less important - although I'll acknowledge that their importance arises from a different context. Variable (and method and class) names are crucial to creating quality software. Poorly named entities (a) make it hard for a reader to understand the code being studied, and (b) make it hard for all, original author or reader, to ensure correctness: without clear and precise names the purpose of an object, method or variable cannot be solidly grasped and, in turn, the correctness of the related code is hard to validate. If you truly want to help beginner coders (.Net or otherwise) then presenting coding practices that are recognised as being valuable would surely be a worthy thing to do. If there are books that still use one character variable names, or any other kind of meaningless names, then we have an even stronger duty to present examples of the use of meaningful names, so that their utility can be noticed. Note: I picked on 'ans' (for 'answer', I presume) because it is meaningless in the context of the algorithm - which is why I proposed 'magicSquare'. ('Answer' is a use-centric concept, which has no value or meaning at the algorithm level.) Sure, this is a trivial program and in this context it is obvious that the array is the magic square; however, in the context of teaching beginners good practices, an algorithm-centric name is likely to be more useful.
 Dim ans(s, s) As Integer What does the ans(s, s) mean. Is ... Peter Hawke20-Dec-11 13:49 Peter Hawke 20-Dec-11 13:49
 Re: Yes....:) NikulDarji20-Dec-11 19:30 NikulDarji 20-Dec-11 19:30
 Reason for my vote of 2 I'll add to the previous dissatisfac... Member 841302020-Dec-11 0:17 Member 8413020 20-Dec-11 0:17
 Re: Don't u think u have to pay attention on logic behind this p... NikulDarji20-Dec-11 2:24 NikulDarji 20-Dec-11 2:24
 Reason for my vote of 1 How about explaining what a 'magic s... pt140118-Dec-11 7:02 pt1401 18-Dec-11 7:02
 Last Visit: 31-Dec-99 19:00     Last Update: 7-Dec-16 19:01 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 14 Dec 2011
Article Copyright 2011 by NikulDarji
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid