Java Program to Print Envelope Star Pattern

Program to Print Envelope Star Pattern

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

Example

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

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

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Approach:

  • Create a function printTriangle to print a triangle.
  • Call the function 4 times to print multiple triangles with star pattern.
  • Print a newline after each iteration.

C Code:

#include <stdio.h>
int printTriangle(int no_triangles, int i, int space)
{ //Function to print triangles
    char star_char = '*';
    int r, c;
    for (r = no_triangles; r >= 1; r--)
    {
        printf("  ");
    }
    for (c = 1; c <= i; c++)
    {
        if (space != 0)
        {
            if (i == 4 && c == 1)
            {
                continue;
            }
        }
        printf("%2c", star_char);
    }
    return 0;
}
int main()
{
    int iter, no_triangles = 4;
    for (iter = 1; iter <= 7; (iter = iter + 2))
    { //Prints the first triangle
        printTriangle(no_triangles, iter, 0);
        no_triangles--;
        printf("\n");
    }
    no_triangles = 5;
    for (iter = 1; iter <= 4; iter++)
    { //Prints the second triangle
        printTriangle(1, iter, 0);
        printTriangle(no_triangles, iter, 1);
        no_triangles = no_triangles - 2;
        printf("\n");
    }
    no_triangles = 1;
    for (iter = 3; iter >= 1; iter--)
    { //Prints the third triangle
        printTriangle(1, iter, 0);
        printTriangle(no_triangles, iter, 0);
        no_triangles = no_triangles + 2;
        printf("\n");
    }
    no_triangles = 1;
    for (iter = 7; iter >= 1; (iter = iter - 2))
    { //Prints the last triangle
        printTriangle(no_triangles, iter, 0);
        no_triangles++;
        printf("\n");
    }
    return 0;
}
Output

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

Related Java Star Patterns Programs: