Programs for printing Pyramid patterns – TechWorld4U09
Table of Contents
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)
Holi 2023 Holi, the festival of colours, marks the beginning of spring and the end of winter. The festival signifies the triumph of good over evil. The colour and vibrancy of the festival represent the … Read more
This time Holi will be on 8 March 2023. Get complete information about Holi 2023 Date in India. Holi is very important in Hinduism, and the whole of India celebrates this festival. This is the … Read more
HTML mcq for gate exam HTML stands for HyperText Markup Language. This is the standard language for building web pages and is used to create the general structure of a website/web page. HTML tells the … Read more
0 Comments