Pattern 139 (Pyramid II)

Pattern 139 (Pyramid II) post thumbnail image

C

#include<stdio.h>

int main()
{
  int min_stars=1;
  /*
    change value of min_stars to set min. no. of stars in
    piramid
    take odd no. for odd no. of stars in each row 1-3-5 etc
    take even no. for even no. stars in each row, 2-4-6 etc
  */
  int p_height=5; //change value to increase or decrease the size of piramid

  int p_space=p_height-1;
  int i,j,k;


  for(i=0; i<p_height; i++)
  {
    for(j=p_space; j>i; j--)
    {
      printf(" ");
    }
    for(k=0; k<min_stars; k++)
    {
      printf("*");
    }
    min_stars+=2;
    printf("\n");
  }
}


/*------- Another Logic -------*/

#include <stdio.h>
int main()
{
  int n = 5; // size

  int px = n; // left print control
  int py = n; // right print control

  int i,j; // loop var

  for(i = 1; i <= n; i++)
  {
    for(j = 1; j < n*2; j++)
    {
      if (j>=px && j<=py)
      {
        printf("*");
      }
      else
      {
        printf(" ");
      }
    }
    px--;
    py++;

    printf("\n");
  }

  return 0;
}

/*--------------------------------*/

C++

 #include<iostream.h>
 
 int main()
 {
 int min_stars=1;  /*change value to set min. no. of stars 
               in piramid
             take odd no. for odd no. of stars in each row 1-3-5 
             etc
             take even no. for even no. stars in each row, 2-4-6 
             etc */
 
 int p_height=5;   //change  value to increase or decrease 
              the size of piramid
 
 
 int p_space=p_height-1;
 
 int i,j,k;
 

 for(i=0;i<p_height;i++){
 for(j=p_space;j>i;j--)
 {
 cout<<" ";
 }
 for(k=0;k<min_stars;k++)
 {
 cout<<"*";
 }
 min_stars+=2;
 cout<<endl;
 }
 return 0;
 }
 

/*-- Another Logic using if-else --*/

#include <iostream.h>

int main()
{
 int n = 5; // size

 int px = n; // left print control
 int py = n; // right print control

 int i,j; // loop var

 for(i = 1; i <= n; i++)
 {
  for(j = 1; j < n*2; j++)
  {
   if (j>=px && j<=py)
   {
    cout<<"*";
   }
   else
   {
    cout<<" ";
   }
  }
  px--;
  py++;
  
  cout<<endl;
 }

return 0;
}

/*--------------------------------*/

Java

  class PatternProg
  {
    public static void main(String args[])
    {
        int min_stars=1;  /*change value to set min. no. of stars
               in piramid
             take odd no. for odd no. of stars in each row 1-3-5
             etc
             take even no. for even no. stars in each row, 2-4-6
             etc */

        int p_height=5;   //change  value to increase or decrease
        the size of piramid

        int p_space=p_height-1;

        for (int i=0; i<p_height; i++)
        {
            for (int j=p_space; j>i; j--)
            {
                System.out.print(" ");
            }
            for (int k=0; k<min_stars; k++)
            {
                System.out.print("*");
            }
            min_stars+=2;
            System.out.println();
        }
    } //end of main
}  //end of class


/*-- Another Logic using if-else --*/

class PatternProg
{
    public static void main(String args[])
    {
        int n = 5; // size

        int px = n; // left print control
        int py = n; // right print control

        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j < n * 2; j++)
            {
                if (j >= px && j <= py)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print(" ");
                }
            }
            px--;
            py++;

            System.out.println();
        }

    }
}
/*--------------------------------*/

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int min_stars = 1; /*change value to set min. no. of stars in piramid
             take odd no. for odd no. of stars in each row 1-3-5 etc
             take even no. for even no. stars in each row, 2-4-6 etc */

    int p_height = 5; //change  value to increase or decrease the size of piramid

    int p_space = p_height - 1;

    for (int i = 0; i < p_height; i++)
    {
      for (int j = p_space; j > i; j--)
      {
        Console.Write(" ");
      }
      for (int k = 0; k < min_stars; k++)
      {
        Console.Write("*");
      }
      min_stars += 2;
      Console.WriteLine();
    }
    Console.ReadKey(true);
  } //end of main
} //end of class


/*-- Another Logic using if-else -

using System;

class PatternProg
 {
  public static void Main()
   {
     int n = 5; // size
     int px = n; // left print control
     int py = n; // right print control

     for (int i = 1; i <= n; i++)
      {
       for (int j = 1; j < n * 2; j++)
        {
         if (j >= px && j <= py)
          {
           Console.Write("*");
          }
         else
          {
           Console.Write(" ");
          }
        }
      px--;
      py++;

      Console.WriteLine();
      }
      Console.ReadKey(true);
   }
 }

/*--------------------------------*/

Python

inc = 1
for x in range(5, 0, -1):
    for y in range(x, 0, -1):
        print(" ", end="")
    print("*" * inc)
    inc += 2;

Tutorial (YouTube)

5 1 vote
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns