Here, we have a basic program example to display an equilateral triangle or pyramid using different languages. This program is created in c language, c++, Java, and Python.
Code to display a pyramid using C language
#include <stdio.h>
void main()
{
int i,j,spc,rows,k;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
spc--;
}
}
Code to display a pyramid using C++ language
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i,j,spc,rows,k;
cout << " Input number of rows: ";
cin >> rows;
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
cout<<"*"<<" ";
cout<<endl;
spc--;
}
}
Code to display a pyramid using Python language
rows = int(input("Enter number of rows: "))
k = 0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k += 1
k = 0
print()
Code to display a pyramid using Java language
import java.util.*;
public class pattern {
public static void main(String[] args) {
int i,j,rows, k=0;
System.out.println("Enter a positive integer: ");
Scanner s=new Scanner(System.in);
rows = s.nextInt();
for (i = 1; i <= rows; ++i, k = 0) {
for (int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
while (k != 2 * i - 1) {
System.out.print("* ");
++k;
}
System.out.println();
}
}
}