patternprogram_128 softethics

Pattern 128 (Pyramid I)

Using 3 Loops / Space Loop Logic

C

#include<stdio.h>


int main()
{
  int n=5;  // size
  int i,j,k;



  for(i=1; i<=n; i++)
  {
    for(j=n-1; j>=i; j--)
    {
      printf(" ");
    }
    for(k=1; k<=i; k++)
    {
      printf("* "); // whitespace after *
    }

    printf("\n");
  }
  return 0;
}

C++

#include<iostream.h>


int main()
{ 
 int n=5;  // size 
 int i,j,k;

  

  for(int i =1;i<=n;i++)
  {
   for(int j =n-1;j>=i;j--)
   {
    cout<<" ";
   }
   for(int k =1;k<=i;k++)
   {
    cout<<"* "; // whitespace after *
   }
   
   cout<<endl;
  }
 return 0;
}

Java

 class PatternProg
 {
   public static void main(String args[])
   {
    int n = 5; // size
    
     for (int i = 1;i <= n;i++)
     {
      for (int j = n - 1;j >= i;j--)
      {
      System.out.print(" ");
      }
      for (int k = 1;k <= i;k++)
      {
      System.out.print("* "); // whitespace after *
      }

      System.out.println();
     }
    
   }
   }
   

C#

using System;

class PatternProg
{
  public static void Main()
  {
    int n = 5; // size

    for (int i = 1; i <= n; i++)
    {
      for (int j = n - 1; j >= i; j--)
      {
        Console.Write(" ");
      }
      for (int k = 1; k <= i; k++)
      {
        Console.Write("* "); // whitespace after *
      }

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

Python

for x in range(1, 6):
    for y in range(5, x, -1):
        print(" ", end="")
    for z in range(0, x):
        print("* ", end="") # whitespace after *
    print()

Using 2 Loops / if-else logic

C

#include <stdio.h>


int main()
{
  int n=5;//size
  int i,j;



  for(i=1; i<=n; i++)
  {
    for(j=n; j>=1; j--)
    {
      if(i>=j)
        printf("* "); // whitespace after *
      else
        printf(" ");
    }
    printf("\n");
  }
  return 0;
}

C++

#include <iostream.h>


int main()
{
 int n=5;//size
 int i,j;
 
 
 
 for(int i =1;i<=n;i++)
 {
  for(int j =n;j>=1;j--)
  {
   if(i>=j)
   cout<<"* "; // whitespace after *
   else
   cout<<" ";
  }
  cout<<endl;
 }
return 0;
}

Java

class PatternProg
{

   public static void main(String args[])
   {
    int n = 5; //size
    
    for (int i = 1;i <= n;i++)
    {
     for (int j = n;j >= 1;j--)
     {
      if (i >= j)
      {
      System.out.print("* "); // whitespace after *
      }
      else
      {
      System.out.print(" ");
      }
     }
     System.out.println();
    }
   
   }

C#

class PatternProg
{
public static void Main()
{
 int n = 5; //size

 for (int i = 1;i <= n;i++)
 {
  for (int j = n;j >= 1;j--)
  {
   if (i >= j)
   {
   Console.Write("* "); // whitespace after *
   }
   else
   {
   Console.Write(" ");
   }
  }
  Console.WriteLine();
 }
 Console.ReadKey(true);
}
}

Python

for x in range(1, 6):
    for y in range(5, 0, -1):
        if x >= y:
            print("* ", end="") # whitespace after *
        else:
            print(" ", end="")
    print()

Single Loop / ONE Loop Logic

C

#include <stdio.h>
int main()
{
	int i,n=5;
	int x=n;
	
	for(i=1;i<=n*n;i++)
	{
	  if(i<x)
      {
	   printf(" ");
      }
	  else
	  {
	   printf("* "); // whitespace after *
	  
	  }
      
	  if(i%n==0)
      {
	   x=x+n-1;
	   printf("\n");
      }
	}
	
 return 0;
}

C++

#include <iostream.h>
int main()
{
	int i,n=5;
	int x=n;
	
	for(i=1;i<=n*n;i++)
	{
	  if(i<x)
      {
	   cout<<" ";
      }
	  else
	  {
	   cout<<"* "; // whitespace after *
	  }
      
	  if(i%n==0)
      {
	   x=x+n-1;
	   cout<<endl;
      }
	}
	
 return 0;
}

Java

class PatternProg
{
   // author Sumit Tiwari
      public static void main (String args[])
      {
          int n = 5, x = n;

          for(int i=1;i<=n*n;i++)
          {
            if(i<x)
            {
               System.out.print(" ");
            }
            else
            {
                System.out.print("* "); // whitespace after *
            }

           if(i%n==0)
           {
            x=x+n-1;
            System.out.println(" ");
          }
        }
      }
}

C#

class PatternProg
{
   // author Sumit Tiwari
      public static void Main ()
      {
          int n = 5, x = n;

          for(int i=1;i<=n*n;i++)
          {
            if(i<x)
            {
               Console.Write(" ");
            }
            else
            {
                Console.Write("* "); // whitespace after *
            }

           if(i%n==0)
           {
            x=x+n-1;
            Console.WriteLine(" ");
           }
        }
        Console.ReadKey(true);
      }
}

Python

n = 5;
p = n;

for x in range(1,n*n+1):
    if x<p:
        print(" ",end="")
    else:
        print("* ", end="") # whitespace after *

    if x%n==0:
        p = p+n-1
        print()

Tutorial (YouTube)

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

[…] Pattern Programs Online […]