Dremendo Tag Line

Print pyramid star pattern using for loop in Java

for Loop - Question 18

In this question, we will see how to print the pyramid star pattern in Java programming using for loop. To know more about for loop click on the for loop lesson.

Q18) Write a program in Java to print the pyramid star pattern using for loop.

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

Program

public class Q18
{
    public static void main(String args[])
    {
        int r,c;

        for(r=1; r<=5; r++)
        {
            // printing the space
            for(c=4; c>=r; c--)
            {
                System.out.print(" ");
            }

            // printing the star
            for(c=1; c<=r; c++)
            {
                System.out.print("* ");		// print the star with a space, it will make a triangle
            }
            System.out.println();
        }
    }
}

Output

    *
   * *
  * * *
 * * * *
* * * * *
video-poster