Write a program for the Pyramid pattern using numbers.
Approach 1: Half Pyramid Pattern
C Program
#include <stdio.h>int main(){int rows;int num;printf("Enter number of rows: ");scanf("%d", &rows);printf("Enter a number which is printed in pattern: ");scanf("%d", &num);for (int i = 1; i <= rows; i++){for (int j = 1; j <= i; j++){printf("%2d", num);}printf("\n");}return 0;}
Output:
Enter number of rows: 5 Enter a number which is printed in pattern: 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
Approach 2: Inverted Half Pyramid Pattern
C Program
#include <stdio.h>int main(){int rows;int num;printf("Enter number of rows: ");scanf("%d", &rows);printf("Enter a number which is to be printed in pattern: ");scanf("%d", &num);for (int i = 1; i <= rows; i++){for (int j = 1; j <= rows - i + 1; j++){printf("%2d", num);}printf("\n");}return 0;}
Output:
Enter number of rows: 5 Enter a number which is to be printed in pattern: 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
Approach 3: Full Pyramid Pattern
C Program
#include <stdio.h>int main(){int n;int num;printf("Enter number of rows: ");scanf("%d", &n);printf("Enter a number which is to be printed in pattern: ");scanf("%d", &num);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("%d", num);}printf("\n");}return 0;}
Output:
Enter number of rows: 5 Enter a number which is to be printed in pattern: 4 4 444 44444 4444444 444444444
Approach 4: Full Inverted Pyramid Pattern
C Program
#include <stdio.h>int main(){int n;int num;printf("Enter number of rows: ");scanf("%d", &n);printf("Enter a number which is to be printed in pattern: ");scanf("%d", &num);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("%d", num);}printf("\n");space++;}return 0;}
Output:
Enter number of rows: 5 Enter a number which is to be printed in pattern: 4 444444444 4444444 44444 444 4
No comments:
Post a Comment