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++

Java

C#

Python

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++

Java

C#

Python

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++

Java

C#

Python

Tutorial (YouTube)

5 1 vote
Rate this Program
Subscribe
Notify of
guest


2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

[…] Pattern Programs Online […]

pradeep

using single loop
1
22
333
4444

55555

plz solve it