Click here to Skip to main content
12,882,639 members (33,974 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 13 Dec 2011

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.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
                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("MAGIC SQUARE:")
        For k As Integer = 0 To size - 1
            For l As Integer = 0 To size - 1
                Console.Write("    " & ans(k, l))
        sum = ans(s \ 2, s \ 2) * s
        Console.Write("SUM: " & sum)
    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.


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 India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: Okay so what does it mean Pin
Peter Hawke22-Dec-11 12:55
memberPeter Hawke22-Dec-11 12:55 
GeneralRe: :) Pin
NikulDarji20-Dec-11 2:40
memberNikulDarji20-Dec-11 2:40 
GeneralRe: I agree, that paying attention to the logic behind the progr... Pin
Member 841302020-Dec-11 1:44
memberMember 841302020-Dec-11 1:44 
GeneralDim ans(s, s) As Integer What does the ans(s, s) mean. Is ... Pin
Peter Hawke20-Dec-11 12:49
memberPeter Hawke20-Dec-11 12:49 
GeneralRe: Yes....:) Pin
NikulDarji20-Dec-11 18:30
memberNikulDarji20-Dec-11 18:30 
GeneralReason for my vote of 2 I'll add to the previous dissatisfac... Pin
Member 841302019-Dec-11 23:17
memberMember 841302019-Dec-11 23:17 
GeneralRe: Don't u think u have to pay attention on logic behind this p... Pin
NikulDarji20-Dec-11 1:24
memberNikulDarji20-Dec-11 1:24 
Don't u think u have to pay attention on logic behind this program instead of what variables used....this site is for developers and this is very simple prog. that the .NET beginners can learn something and use it if they want and for your kind information refer any reference book of any author they also used such types of variables... especially for explain the ARRAY....hope u understands these things.....Smile | :)
GeneralReason for my vote of 1 How about explaining what a 'magic s... Pin
pt140118-Dec-11 6:02
memberpt140118-Dec-11 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

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