Write the code to take a string and print it diagonally. – Java Program to Print Hollow Square with Diagonal Star Pattern

Program to Print Hollow Square with Diagonal Star Pattern

Write the code to take a string and print it diagonally: In this article we are going to see how to print the hollow square with diagonal star pattern.

Example-1

When side value=7
*******
**    **
* *  * *
*   *  *
* *  * *
**    **
*******
Example-2:

When side value=10
**********
**         **
* *       * *
*  *     *  *
*    **     *
*    **     *
*  *     *  *
* *       * *
**         **
**********

Now, let’s see the actual program to print it.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Approach:

  • Enter the side of the square and store it in an integer variable side.
  • Take outer for loop to iterate the side.
  • Take second/inner for loop to check the condition and print.
  • Then go on printing the star symbols according to the iteration.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        Scanner scan = new Scanner(System.in);
        System.out.print("Side of the square : ");
        //Taking side of the square as input from user
        int r, c, side=scan.nextInt();

    //Outer loop
    for (r = 1; r <= side; ++r)
    {
        for (c = 1; c <= side; ++c)
        {
            //Inner for loop
            if ((r == 1) || (r == side) || (c == 1) || (c == side) || (r == c) || (c == (side - r + 1)))
                //Condition to check to print star
                System.out.print("*");
            else
                System.out.print(" ");
        }
            //Prints a new line
            System.out.println();
        }
    }  
}

Output

Side of the square : 7
*******
**    **
* *  * *
*   *  *
* *  * *
**    **
*******

Method-2 : User Input Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        int r, c,side;
        char ch;
        Scanner scan = new Scanner(System.in);
        System.out.print("Side of the square : ");
        //Taking side of the square as input from user
        side=scan.nextInt();
        System.out.print("Enter any random character : ");
        //Taking any character as input from user
        ch=scan.next().charAt(0);

    //Outer loop
    for (r = 1; r <= side; ++r)
    {
        for (c = 1; c <= side; ++c)
        {
            //Inner for loop
            if ((r == 1) || (r == side) || (c == 1) || (c == side) || (r == c) || (c == (side - r + 1)))
                //Condition to check to print star
                System.out.print(ch);
            else
                System.out.print(" ");
        }
            //Prints a new line
            System.out.println();
        }
    }  
}

Output

Side of the square : 7
Enter any random character : *
*******
**    **
* *  * *
*   *   *
* *  * *
**    **
*******

Explanation :

Let’s understand the program by going through the detailed explanation.

We have taken side value as 7.

Iteration-1
r=1 (passes the first for loop condition) because it will execute until r<=side
Now as r=1 so inner for loop will print 7 stars.

*******

Iteration-2
r=2 (passes the first for loop condition) because it will execute until r<= side
Now it will print two stars for c==1 and r==c, then it will print three spaces for else condition. After that it will print two more stars for c==(side-r+1) and c==side.

**   **

Iteration-3
r=3 (passes the first for loop condition) because it will execute until r<= side
Now it will print one star for c==1, one space for else statement, then another star for r==c followed by a space. Then it will print a star for c==(side-r+1) another space and then a star for c==side.

* * * *

Iteration-4

r=4 (passes the first for loop condition) because it will execute until r<= side
Now it will print one star for c==1, two space for else statement, then another star for r==c followed by two spaces. Then it will print a star for c==side.

*  *  *

Iteration-5

r=5 (passes the first for loop condition) because it will execute until r<= side
Now it will print one star for c==1, one space for else statement, then another star for c==(side-r+1) followed by a space. Then it will print a star for r==c another space and then a star for c==side.

* * * *

Iteration-6
r=6 (passes the first for loop condition) because it will execute until r<= side
Now it will print two stars for c==1 and c==(side-r+1), then it will print three spaces for else condition. After that it will print two more stars for r==c and c==side.

**   **

Iteration-7
r=7 (passes the first for loop condition) because it will execute until r<=side
Now as r=side so inner for loop will print 7 stars.

*******

Now r=8, so first for loop condition will fail.  So next for loop will not be executed any more.

Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.

*******
**    **
* *  * *
*   *   *
* *  * *
**    **
*******

C Code:

#include <stdio.h>
int main(int argc, char const *argv[])
{
    printf("Side of the square : ");
    //Taking side of the square as input from user
    int side, r, c;
    scanf("%d", &side);

    //Outer loop
    for (r = 1; r <= side; ++r)
    {
        for (c = 1; c <= side; ++c)
        {
            //Inner for loop
            if ((r == 1) || (r == side) || (c == 1) || (c == side) || (r == c) || (c == (side - r + 1)))
                //Condition to check to print star
                printf("*");
            else
                printf(" ");
        }
        //Prints a new line
        printf("\n");
    }
    return 0;
}

Output

Side of the square : 7
*******
**    **
* *  * *
*   *   *
* *  * *
**    **
*******

C++ Code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Side of the square : ";
    //Taking height Of Tree as input from user
    int side, r, c;
    cin >> side;

    //Outer loop
    for (r = 1; r <= side; ++r)
    {
        for (c = 1; c <= side; ++c)
        {
            //Inner for loop
            if ((r == 1) || (r == side) || (c == 1) || (c == side) || (r == c) || (c == (side - r + 1)))
                //Condition to check to print star
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Output

Side of the square : 7
*******
**    **
* *  * *
*   *   *
* *  * *
**    **
*******

Related Java Star Pattern Programs: