Java Program to Print Kite Star Pattern

Program to Print Kite Star Pattern

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

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Approach:

  • Take first for loop to print one pyramid.
  • Take a second for loop to print a inverted pyramid.
  • Take a third for loop to print a small pyramid.
  • The inner loop of all three loops will print column values

JAVA Code:

Method-1 : Static Star Character

import java.io.*;
 
public class Main
{
public static void main(String[] args)
{
 
    int r, c, z, sp, space = 4;
    // loop to print one pyramid
    for (r = 1; r <= 5; r++)
    {
 
        // to print space
        for (sp = space; sp >= 1; sp--)
        {
            System.out.print(" ");
        }
 
        // to print *
        for (c = 1; c <= r; c++)
        {
            System.out.print("*");
        }
 
        for (z= 1; z <= (r - 1); z++)
        {
            if (r == 1)
            {
                continue;
            }
 
            System.out.print("*");
        }
        System.out.println();
        space--;
    }
 
    space = 1;
    
    // loop to print one inverted pyramid
    for (r = 4; r >= 1; r--)
    {
        for (sp = space; sp >= 1; sp--)
        {
            System.out.print(" ");
        }
 
        for (c = 1; c <= r; c++)
        {
          System.out.print("*");
        }
 
        for (z = 1; z <= (r - 1); z++)
        {
            System.out.print("*");
        }
 
        space++;
        System.out.println();
    }
 
    space = 3;
 
    // loop to print one small pyramid
    for (r = 2; r <= 5; r++)
    {
        if ((r % 2) != 0)
        {
            for (sp = space; sp >= 1; sp--)
            {
                System.out.print(" ");
            }
 
            for (c = 1; c <= r; c++)
            {
                System.out.print("*");
            }
        }
 
        if ((r % 2) != 0)
        {
            System.out.println();
            space--;
        }
    }
}
}
Output:

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

Method-2 : User Input Character

import java.io.*;
import java.util.*;
 
public class Main
{
public static void main(String[] args)
{
 
    int r, c, z, sp, space = 4;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter any random character : ");
    char s=sc.next().charAt(0);
    // loop to print one pyramid
    for (r = 1; r <= 5; r++)
    {
 
        // to print space
        for (sp = space; sp >= 1; sp--)
        {
            System.out.print(" ");
        }
 
        // to print *
        for (c = 1; c <= r; c++)
        {
            System.out.print(s);
        }
 
        for (z= 1; z <= (r - 1); z++)
        {
            if (r == 1)
            {
                continue;
            }
 
            System.out.print(s);
        }
        System.out.println();
        space--;
    }
 
    space = 1;
    
    // loop to print one inverted pyramid
    for (r = 4; r >= 1; r--)
    {
        for (sp = space; sp >= 1; sp--)
        {
            System.out.print(" ");
        }
 
        for (c = 1; c <= r; c++)
        {
          System.out.print(s);
        }
 
        for (z = 1; z <= (r - 1); z++)
        {
            System.out.print(s);
        }
 
        space++;
        System.out.println();
    }
 
    space = 3;
 
    // loop to print one small pyramid
    for (r = 2; r <= 5; r++)
    {
        if ((r % 2) != 0)
        {
            for (sp = space; sp >= 1; sp--)
            {
                System.out.print(" ");
            }
 
            for (c = 1; c <= r; c++)
            {
                System.out.print(s);
            }
        }
 
        if ((r % 2) != 0)
        {
            System.out.println();
            space--;
        }
    }
}
}
Output:

Enter any random character : @
                @
            @@@
        @@@@@
    @@@@@@@
@@@@@@@@@
   @@@@@@@
       @@@@@
           @@@
              @
          @@@
      @@@@@

Explanation

Let’s understand the program with detailed explanation.

Iteration to print first pyramid

Iteration-I

r=1 (passed through first for loop condition) which will execute till r<=5. Inside a nested loop print spaces from space till 1 i.e. 4 spaces. Inside another nested loop print symbol from 1 till r  and whenever a variable say z<= r-1 skip iteration don’t print symbol i.e. total 1 star.

    *

Iteration-II

r=2 (passed through first for loop condition) which will execute till r<=5. Inside nested loop print spaces from space till 1 i.e. 3 spaces. Inside another nested loop prints symbol from 1 till r and whenever a variable say z<= r-1 skip iteration don’t print symbol i.e. total 3 star.

   ***

Iteration-III

r=3 (passed through first for loop condition) which will execute till r<=5. Inside nested for loop print spaces from space till 1 i.e. 2 spaces. Inside another nested loop prints symbol from 1 till r and whenever a variable say z<= r-1 skip iteration don’t print symbol i.e. total 5 star.

  *****

Iteration-IV

r=4 (passed through first for loop condition) which will execute till r<=5. Inside nested for loop print spaces from space till 1 i.e. 1 space. Inside another nested loop prints symbol from 1 till r and whenever a variable say z<= r-1 skip iteration don’t print symbol i.e. total 7 star.

 *******

Iteration-V

r=5 (passed through first for loop condition) which will execute till r<=5. Inside nested for loop print spaces from space till 1 i.e. 0 space. Inside another nested loop prints symbol from 1 till r and whenever a variable say z<= r-1 skip iteration don’t print symbol i.e. total 9 star.

*********

Now r=6, so first for loop condition fails. And no more for loop will be executed. So we see a pattern like this.

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

Iteration to print the invert pyramid

Iteartion-I

r=4 (passed through first for loop condition) which will execute till r>=1. Taking a nested loop iterate a variable sp initially assigned with space=1 till sp<=1, if condition satisfies print spaces i.e. 1 space. Taking another for loop iterate c from 1 till c<=r similarly taking another nested loop iterate variable z till z<= r-1 print the symbols i.e. 7 stars. Increment space.

 *******

Iteartion-II

r=3 (passed through first for loop condition) which will execute till r>=1. Taking a nested loop iterate a variable sp initially assigned with space=2 till sp<=1, if condition satisfies print spaces i.e. 2 spaces. Taking another for loop iterate c from 1 till c<=r similarly taking another nested loop iterate variable z till z<= r-1 print the symbols i.e. 5 stars. Increment space.

  *****

Iteartion-III

r=2 (passed through first for loop condition) which will execute till r>=1. Taking a nested loop iterate a variable sp intitally assigned with space=3 till sp<=1, if condition satisfies print spaces i.e. 3 spaces. Taking another for loop iterate c from 1 till c<=r similarly taking another nested loop iterate variable z till z<= r-1 print the symbols i.e. 3 stars. Increment space.

   ***

Iteartion-IV

r=1 (passed through first for loop condition) which will execute till r>=1. Taking a nested loop iterate a variable sp initially assigned with space=4 till sp<=1, if condition satisfies print spaces i.e. 4 spaces. Taking another for loop iterate c from 1 till c<=r similarly taking another nested loop iterate variable z till z<= r-1 print the symbols i.e. 1 star. Increment space.

    *

Now r=0, so first for loop condition fails. And no more for loop will be executed. So we see a pattern like this.

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

Iteration to print the a small pyramid

Iteration-I

r=2 (passed through first for loop condition) which will execute till r<=5. Check if r%2!=0 , since the condition fails here so no code will be executed, hence no output will be shown.

Iteration-II

r=3 (passed through first for loop condition) which will execute till r<=5. Check condition r%2!=0. Since true, take a nested for loop and iterate sp assigned with space as 3 till sp>=1, print spaces i.e. 3 spaces. Iterate another nested loop from c=1 till c<=r, print symbols i.e. 3 stars. Decrement space.

    ***

Iteration-III

r=4 (passed through first for loop condition). Check if r%2!=0 , since the condition fails here so no code will be executed, hence no output will be shown.

Iteration-IV

r=5 (passed through first for loop condition) which will execute till r<=5. Check condition r%2!=0. Since true, take a nested for loop and iterate sp assigned with space as 2 till sp>=1, print spaces i.e. 2 spaces. Iterate another nested loop from c=1 till c<=r, print symbols i.e. 5 stars. Decrement space.

  *****

Now r=6, so first for loop condition fails. And no more for loop will be executed. At last we see a pattern like this.

   ***
  *****
  • At last the following output will be shown on the screen.
     *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
   ***
  *****

C Code:

#include <stdio.h>

int main()
{
    int r, c, z, sp, space = 4;
    for (r = 1; r <= 5; r++)
    {
        for (sp = space; sp >= 1; sp--)
        {
                printf(" ");
        }
        for (c = 1; c <= r; c++)
        {
                printf("*");
        }
 
        for (z= 1; z <= (r - 1); z++)
        {
            if (r == 1)
            {
                continue;
            }
 
                printf("*");
        }
                printf("\n");
        space--;
    }
 
    space = 1;
    for (r = 4; r >= 1; r--)
    {
        for (sp = space; sp >= 1; sp--)
        {
                printf(" ");
        }
 
        for (c = 1; c <= r; c++)
        {
                printf("*");
        }
 
        for (z = 1; z <= (r - 1); z++)
        {
                printf("*");
        }
 
        space++;
                printf("\n");
    }
 
    space = 3;
 
    for (r = 2; r <= 5; r++)
    {
        if ((r % 2) != 0)
        {
            for (sp = space; sp >= 1; sp--)
            {
                printf(" ");
            }
 
            for (c = 1; c <= r; c++)
            {
                printf("*");
            }
        }
 
        if ((r % 2) != 0)
        {
                printf("\n");
            space--;
        }
    }
}

Output
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
   ***
  *****

C++ Code:

// C++ Program to print Kite Pattern
#include <bits/stdc++.h>
#include <stdlib.h>

using namespace std;
int main()
{

    int i, j, k, sp, space = 4;
    char prt = '$';

    for (i = 1; i <= 5; i++)
    {
        for (sp = space; sp >= 1; sp--)
        {
            cout << " ";
        }

        // For printing the $
        for (j = 1; j <= i; j++)
        {
            cout << prt;
        }

        for (k = 1; k <= (i - 1); k++)
        {
            if (i == 1)
            {
                continue;
            }

            cout << prt;
        }
        cout << "\n";
        space--;
    }

    space = 1;

    for (i = 4; i >= 1; i--)
    {
        for (sp = space; sp >= 1; sp--)
        {
            cout << " ";
        }

        for (j = 1; j <= i; j++)
        {
            cout << prt;
        }

        for (k = 1; k <= (i - 1); k++)
        {
            cout << prt;
        }

        space++;
        cout << "\n";
    }

    space = 3;

    for (i = 2; i <= 5; i++)
    {
        if ((i % 2) != 0)
        {
            for (sp = space; sp >= 1; sp--)
            {
                cout << " ";
            }

            for (j = 1; j <= i; j++)
            {
                cout << prt;
            }
        }

        if ((i % 2) != 0)
        {
            cout << "\n";
            space--;
        }
    }

    return 0;
}

Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
   ***
  *****

Related Java Star Pattern Programs: