Programs for printing Pyramid patterns – TechWorld4U09

Programs for printing Pyramid patterns – TechWorld4U09

Programs for printing Pyramid patterns in C

#include <stdio.h>
int main() {
   int i, j, rows;
   printf("Enter the number of rows: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; ++i) {
      for (j = 1; j <= i; ++j) {
         printf("* ");
      }
      printf("\n");
   }
   return 0;
}

Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Programs for printing Pyramid patterns in Java

import java.io.*;
 
// Java code to demonstrate star patterns
public class GeeksForGeeks
{
    // Function to demonstrate printing pattern
    public static void printStars(int n)
    {
        int i, j;
 
        // outer loop to handle number of rows
        //  n in this case
        for(i=0; i<n; i++)
        {
 
            //  inner loop to handle number of columns
            //  values changing acc. to outer loop   
            for(j=0; j<=i; j++)
            {
                // printing stars
                System.out.print("* ");
            }
 
            // ending line after each row
            System.out.println();
        }
   }
 
    // Driver Function
    public static void main(String args[])
    {
        int n = 5;
        printStars(n);
    }
}

Output


* 
* * 
* * * 
* * * * 
* * * * * 

Program for printing Pyramid patterns in Python

def pypart(n):
     
    # outer loop to handle number of rows
    # n in this case
    for i in range(0, n):
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing stars
            print("* ",end="")
      
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
pypart(n)

Output

* 
* * 
* * * 
* * * * 
* * * * * 

Leave a Comment

Your email address will not be published. Required fields are marked *