Pyramid star pattern

Pyramid star pattern

To print the Pyramid star pattern we need two loop. first is outer loop and second is inner loop.

first loop that is the outer loop works on the row or line and the another loop that is inner loop works on the column mean how many start you want to put in the single line.so here we need to now proper working of the loop that we can easily make a desired pattern.

Now we will discuss all possible star pattern using java. 

1. Write a Program to print the following pattern.

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

Working

Step 1- first you need to initialize two variable for loop.

Step 2- the first loop  for the row.

Step 3-  the second loop work for the column.

Step 4- now print the *.

Step 5- now out side of loop, put new line.

Step 5- stop.

#include<stdio.h>
int main()
{
    int i, j;
    for(i=1; i<=5; i++)
    {
        for(j=1; j<=i; j++)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
public class Main
{
       public static void main(String[] args) 
       {
             int i,j;
             //this loop work on the row 
             for(i=0;i<5;i++)
             {
                   //this loop work on the column 
                   for(j=1;j<=i;j++)
                   {
                         System.out.print("* ");
                   }
                   System.out.println();
              }
        }
}
for i in range(5):
    for j in range(i+1):
        print(“*”,end=” “)
    print()
for i in range(5):print(“* “*(i+1))
#include<iostream.h>
using namespace std;
int main()
{
int i, j;

for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
cout<<"* ";
}

cout<<"\n";
}
return 0;
}

2. Write a program to print the following pattern

* 
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include<stdio.h>
int main()
{
    int i, j, k=1;
    for(i=1; i<=5; i++)
   {
        for(j=1; j<=k; j++)
        {
            printf("* ");
        }
        printf("\n");
        k = k + 2;
    }
    return 0;
}
public class Main
{
	public static void main(String[] args) {
		int i,j,k=1;
//this loop work on the row for(i=0;i<5;i++) {
//this loop work on the column
//here we want to print * two more than previous row
//so this loop work on the k for(j=0;j<k;j++) System.out.print(" * ");
//increment k with 2 k=k+2; System.out.println(); } }
for i in range(5):
    for j in range(2*i+1):
        print(“*”,end=” “)
    print()
for i in range(5):print(“* “*(2*i+1))
#include<iostream>
using namespace std;
int main()
{
int i, j, k=1;
for(i=1; i<=5; i++)
{
for(j=1; j<=k; j++)
{
cout<<"* ";
}

cout<<endl;
k = k + 2;
}
return 0;
}

3. Write a program to print the following pattern

        * 
* *
* * *
* * * *
* * * * *
#include <stdio.h>  
int main()  
{  
    int n,m=1;    
    for(int i=5;i>=1;i--)  
    {  
        for(int j=1;j<=i-1;j++)  
        {  
        printf(" "); 
       } 
        for(int k=1;k<=m;k++)  
       { 
            printf("*");  
       } 
        printf("\n");  
        m++;  
     }  
     return 0;  
}  
public class Main
{
    public static void main(String[] args)
    {
          int i,j,k=8;
//this loop work on the row
           for(i=0;i<5;i++)
           {
//this loop work for spaces
                  for(j=0;j<k;j++)
                          System.out.print(" ");
//decrement k for reduce the spaces in next row
                  k=k-2;
//this loop work for print the *
                  for(j=0;j<=i;j++)
                          System.out.print("* ");
                  System.out.println();
             }
       }
}
n=5
for i in range(n):
    for j in range(n): 
        if(i+j<n-1):
            print(” “,end=“”)
        else:
            print(“*”,end=“”)
    print()
for i in range(5):print(” “*(5-i-1)+“*”*(i+1))
#include<iostream>
using namespace std;

int main()
{
int n,m=1;
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i-1;j++)
{
cout<<" ";
}
for(int k=1;k<=m;k++)
{
cout<<"*";
}
cout<<endl;
m++;
}
return 0;
}

4. Write the code to print the following patter

                * 
          * * *
       * * * * *
    * * * * * * *
* * * * * * * * *
#include <stdio.h>  
int main()  
{  
    int i,j,k=16,s=1;
   for(i=0;i<5;i++)
    {
        for(j=0;j<k;j++)
        {
            printf(" ");
        }       
        k=k-4;        
        for(j=1;j<=s;j++)
       {
            printf("* ");
        }            
        s=s+2;
       printf("\n");
    }
    return 0;
}  
public class Main
{
     public static void main(String[] args)
     {
           int i,j,k=16,s=1;
//this loop work on the row
           for(i=0;i<5;i++)
           {
//this loop work on space
                for(j=0;j<k;j++)
                    System.out.print(" ");
//decrement k for reduce the spaces in the next row
                k=k-4;
//this loop work on the column
//here we want to print * two more than the previous row
//so this loop work on the k
                for(j=1;j<=s;j++)
                     System.out.print("* ");
//increment s with 2
                 s=s+2;
                 System.out.println();
            }
       }
}
n=5
for i in range(5):
    for j in range(2*n+1):
        if(j<(2*n-1)-(2*i-1)):  
            print(” “,end=“”)
        else:
            print(“*”,end=“”)
    print()
for i in range(5):print(” “*((5*21)-(2*i-1))+“*”*(2*i+1)) 
#include<iostream>
using namespace std;

int main()
{
int i,j,k=16,s=1;

for(i=0;i<5;i++)
{
for(j=0;j<k;j++)
{
cout<<" ";
}
k=k-4;
for(j=1;j<=s;j++)
{
cout<<"* ";
}
s=s+2;
cout<<endl;
}
return 0;
}

5. Write the code to print the following pattern.

    *
   ***
  *****
 *******
*********
#include <stdio.h>  
int main()  
{  
    int i,j,k=0,row=5;
    //this loop work on the row
    for (i=1;i<=row;i++)
    {
        //this loop work for both the space and print *
        for(j=1;j<=row-i;j++)
        printf(" ");
        //until value of the k is not equal to 2*i-i, it print star
        while(k!=(2*i-1))
        {
            printf("*");
            k++;
        }
        k=0;
        printf("\n");
    }
    return 0;
}  
public class Main
{
   public static void main(String[] args)

   {
       int i,j,k=0,row=5;

      //this loop work on the row
       for (i=1;i<=row;i++)
      {

           //this loop work for both the space and print *
            for(j=1;j<=row-i;j++)
                    System.out.print(" ");

           //until value of the k is not equal to 2*i-i, it print star
            while(k!=(2*i-1))
            {
                  System.out.print("*");
                   k++;
            }
           k=0;
          System.out.println();
       }
    }
}
n=5  
for i in range(5):
    for j in range(2*n+1):
        if(j<n-i-1):       
            print(” “,end=“”)
        else:
            print(“*”*(2*i+1),end=“”)
            break
    print()
for i in range(5):print(” “*(5-i-1)+“*”*(2*i+1))
#include<iostream>
using namespace std;

int main()
{
int i,j,k=0,row=5;
//this loop work on the row
for (i=1;i<=row;i++)
{
//this loop work for both the space and print *
for(j=1;j<=row-i;j++)
cout<<" ";
//until value of the k is not equal to 2*i-i, it print star
while(k!=(2*i-1))
{
cout<<"*";
k++;
}
k=0;
cout<<endl;
}
return 0;
}  

4 comments on “Pyramid star pattern”


  • Vyas

    For * pyramid printing
    #include
    using namespace std;
    int main()
    {
    int i,j,k=0,row=5;

    for (i=1;i<=row;i++)
    {

    for(j=1;j<=row-i;j++)
    printf(" ");
    for(k=1;k<=(2*i-1);k++)
    printf("*");
    printf("\n");
    }
    return 0;
    }


  • Nishant

    QUESTION:5 Solutions
    public class Star
    {
    public static void main(String args[])
    {
    int i,k=9,p=1;
    for(i=0;i<5;i++) //For the row
    {
    for(int z=0;z<k;z++) // for the spaces in a row
    System.out.print(" ");

    k= k-1;
    for(int j=0;j<p;j++) // for printing the *
    {
    System.out.print("*");
    }
    p = p + 2;

    System.out.println();
    }
    }
    }


    • HelpPrepInsta

      Thank you for your appreciation…. We also want you to know that we are happy to help you and serve you better!!!