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("*");
}
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<<"*";
}
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("*");
}
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("*");
}
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="")
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("*");
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<<"*";
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("*");
}
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("*");
}
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="")
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("*");
}
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<<"*";
}
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("*");
}
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("*");
}
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="")
if x%n==0:
p = p+n-1
print()
[…] Pattern Programs Online […]