Write a program to print all types of pyramids using stars.
Approach 1: Half Pyramid Pattern
C Program
#include <stdio.h>int main(){int rows;printf("Enter number of rows: ");scanf("%d", &rows);for (int i = 1; i <= rows; i++){for (int j = 1; j <= i; j++){printf("* ");}printf("\n");}return 0;}
Output:
Enter number of rows: 5 * * * * * * * * * * * * * * *
Approach 2: Inverted Half Pyramid Pattern
C Program
#include <stdio.h>int main(){int rows;printf("Enter number of rows: ");scanf("%d", &rows);for (int i = 1; i <= rows; i++){for (int j = 1; j <= rows - i+1; j++){printf("* ");}printf("\n");}return 0;}
Output:
Enter number of rows: 5 * * * * * * * * * * * * * * *
Approach 3: Full Pyramid Pattern
C Program
#include <stdio.h>int main(){int n;printf("Enter number of rows: ");scanf("%d", &n);for (int i = 0; i < n; i++){for (int j = 0; j < n - 1 - i; j++){printf(" ");}for (int j = n - 1 - i; j <= n - 1 + i; j++){printf("*");}printf("\n");}return 0;}
Output:
Enter number of rows: 5 * *** ***** ******* *********
Approach 4: Full Inverted Pyramid Pattern
C Program
#include <stdio.h>int main(){int n;printf("Enter number of rows: ");scanf("%d", &n);int space = 0;for (int i = 0; i < n; i++){for (int j = 0; j < space; j++){printf(" ");}for (int j = 1; j <= 2 * (n - i) - 1; j++){printf("*");}printf("\n");space++;}return 0;}
Output:
Enter number of rows: 5 ********* ******* ***** *** *
No comments:
Post a Comment