Java Program to Print Asterisk Star Pattern

Program to Print Asterisk Star Pattern

In this article we are going to see how to print the asterisk star pattern

Example-1

When row value=4

*   *   *
 *  *  *
  * * *
   ***
    *
   ***
  * * *
 *  *  *
*   *   *
Example-2:

When row value=10

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

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

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Approach:

  • Enter length of the line and store it in an integer variable size.
  • Take outer for loop to iterate the rows.
  • Take inner for loop, to print space and star.
  • After each iteration print a newline.

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("Stars : ");
        // Taking number of stars in a segment 
        // as input from user
        int stars=scan.nextInt(),r,c;
        //Outer Loop
        for(r=1;r<2*stars;r++)
        {
            //Inner loop
            for(c=1;c<2*stars;c++)
            {
                if(c==stars || r==c || r+c==2*stars)
                        System.out.print("*");
                else
                        System.out.print(" ");  
            }
            //Prints a newline
            System.out.println( );  
        }
    }
}

Output

Stars : 8

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

Method-2 : User Input Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args) 
    {
        int r,c;
        Scanner scan = new Scanner(System.in);
        
        // Taking number of stars in a segment 
        // as input from user
        System.out.print("Stars : ");
        int stars=scan.nextInt();
        
         System.out.print("Character : ");
        char character=scan.next().charAt(0);
        
        //Outer Loop
        for(r=1;r<2*stars;r++)
        {
            //Inner loop
            for(c=1;c<2*stars;c++)
            {
                if(c==stars || r==c || r+c==2*stars)
                        System.out.print(character);
                else
                        System.out.print(" ");  
            }
            //Prints a newline
            System.out.println( );  
        }
    }
}

Output

Stars : 8

Character : *

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

C Code:

#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
    printf("Stars : ");
    //Taking number of stars in a segment as input from user
    int stars, r, c;
    float d;
    scanf("%d", &stars);

    //Outer loop
    for (r = 1; r < 2 * stars; r++)
    { 
        //Inner loop
        for (c = 1; c < 2 * stars; c++)
        { 
            if (c == stars || r == c || r + c == 2 * stars)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
        //Prints a newline
    }
    return 0;
}


Output

Stars : 8

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

C++ Code:

#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Stars : ";
    //Taking number of stars in a sefment as input from user
    int stars, r, c;
    cin >> stars;

    //Outer loop
    for (r = 1; r < 2 * stars; r++)
    { 
        //Inner loop
        for (c = 1; c < 2 * stars; c++)
        { 
            if (c == stars || r == c || r + c == 2 * stars)
                cout << "*";
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}


Output

Stars : 8

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

Related Java Star Pattern Programs: