Magic Square

Discussion in 'C++' started by asadullah.ansari, Feb 6, 2008.

  1. alramesh

    alramesh New Member

    Joined:
    Feb 5, 2008
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    Good info!!!
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
  3. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    i think the code has some problem
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,365
    Likes Received:
    385
    Trophy Points:
    83
    The criteria is not how much you work on it but how much you have provided in the short possible span
     
  5. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

    Why you are thinking ? Just check and then if problem came then tell me . 100% i will solve.
    I will be more pleasure If you got more issues in this program? welcome but first check
    this program please...
     
  6. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    :iagree:
     
  7. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    the program works absolutely fine please check
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,365
    Likes Received:
    385
    Trophy Points:
    83
  9. nileshkumar_1982

    nileshkumar_1982 New Member

    Joined:
    Feb 6, 2008
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Nice article . I will vote for this.
     
  10. mindblaster

    mindblaster New Member

    Joined:
    Jun 20, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    this is doesnt work :(
     
  11. deniz

    deniz New Member

    Joined:
    Oct 17, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    so, where can i get the solution of this tedious magic square program?..
     
  12. deniz

    deniz New Member

    Joined:
    Oct 17, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    public class MagicSquare {
    
        public static void main(String[] args) { 
            int N = Integer.parseInt(args[0]);
            if (N % 2 == 0) throw new RuntimeException("N must be odd");
    
            int[][] magic = new int[N][N];
    
            int row = N-1;
            int col = N/2;
            magic[row][col] = 1;
    
            for (int i = 2; i <= N*N; i++) {
                if (magic[(row + 1) % N][(col + 1) % N] == 0) {
                    row = (row + 1) % N;
                    col = (col + 1) % N;
                }
                else {
                    row = (row - 1 + N) % N;
                    // don't change col
                }
                magic[row][col] = i;
            }
    
            // print results
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    if (magic[i][j] < 10)  System.out.print(" ");  // for alignment
                    if (magic[i][j] < 100) System.out.print(" ");  // for alignment
                    System.out.print(magic[i][j] + " ");
                }
                System.out.println();
            }
    
        }
    }
    
     
    Last edited by a moderator: Jan 2, 2010
  13. deniz

    deniz New Member

    Joined:
    Oct 17, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Shabbir, could you check this one? I'm not quite sure if this gonna work out,eh.. Thanks..
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,365
    Likes Received:
    385
    Trophy Points:
    83
    For What?
     
  15. deniz

    deniz New Member

    Joined:
    Oct 17, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    i dont know how to create a magic square program in java. i need it badly..thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice