Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:15:11+00:00 2026-06-06T18:15:11+00:00

The problem is to create concentric and nested squares,starting with the largest square’s side

  • 0

The pattern of stars that need to be printed The problem is to create concentric and nested squares,starting with the largest square’s side given as N.Its just like drawing one square inside another ON PAPER,until no more squares are possible to be drawn, by decrementing the length of the side by 4 after each square(2 from startPos of side and 2 from endPos); And N is the size of the square to start with.
You have to draw the sides by using multiplication symbol(‘*’).

The proportion(looks more like rectangles than squares) of the above image might not be exact, but it would give u idea about what needs to be done ..
The below code is what I have tried…This the exact pattern that needs to be printed..

public static void main(String[] args)
{
    int N=9;
    int iLo=0;
    int iHi=N-1;
    int jLo=0;
    int jHi=N-1;

    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(i==0 || (i==N-1) || (j==0) || (j==N-1))
                System.out.print('*');
            else
            {
                if(i<=N/2)
                {
                    if((i%2==0) && (i>=iLo) && (i<=iHi) && (j>=jLo) && (j<=jHi))
                        System.out.print('*');
                    else
                        if(i==iLo || i==iHi )
                            System.out.print('*');
                        else
                            System.out.print(' ');  
                }
                else
                {
                    if((i%2==0) && (i>=iLo) && (i<=iHi) && (j>=jLo) && (j<=jHi))
                            System.out.print('*');
                        else
                            System.out.print(' ');  
                }

            }

        }
        System.out.print("  i-->"+i+"   iLo-->"+iLo+" iHi-->"+iHi+" jLo-->"+jLo+" jHi-->"+jHi);
        if(i%2>0)
        {
            if(i<=N/2)
            {
                jLo=jLo+2;
                jHi=jHi-2;
                iLo=iLo+1;
                iHi=iHi-1;
            }
            else
            {
                jLo=jLo-2;
                jHi=jHi+2;
                iLo=iLo-1;
                iHi=iHi+1;
            }
        }
        else
        {

        }

        System.out.println();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:15:13+00:00Added an answer on June 6, 2026 at 6:15 pm

    The following code offers the simplest recursive solution possible—

    public class SquaresInSquare {

    private static char[][] arr;
    
    
    public static void populate(int start,int N)
    {
        //System.out.println(start+" "+N);
        if(N < 0 || start > N)
            return;
    
        for (int i = start; i < N; i++)
        {
            for (int j = start; j < N; j++)
            {
                if(i==start || i==N-1 || j==start || j==N-1)
                    arr[i][j]='*';
                else
                    arr[i][j]=' ';
            }
        }
    
        populate(start+2,N-2);
    }
    
    public static void print()
    {
        for (int i = 0; i < arr.length; i++)
        {
            for (int j = 0; j < arr.length; j++)
            {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
    
    /**
     * @Method :main is Personal Method Of Class-->SquaresInSquare
     * @returns : void
     * @param args
     */
    public static void main(String[] args)
    {
        int N=21;
        arr=new char[N][N];
        populate(0,N);
        print();
    
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem: Create a constructor for a telephone number given a string
I'm having trouble solving this problem: Create a function that given a character set
got a strange problem: I create a TPanele at runtime and change its color
The problem: To create a recursive array function that walks through a three dimensional
I have a problem to create a preprocessor macro function, that concatenates two Strings
I have a problem for create a SQL job, in my local machine I
How can I solve this problem Cannot Create Forms. No MDI forms are currently
OK, here is how to re-create the problem had: Create a new project, using
I have the problem when I create blank wpf .Net 3.5 project. Visual studio
I have a problem with the Create View in the SimpleRepository example in Subsonic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.