C Program to Print Butterfly Pattern

Write a Program to print the below pattern for the given input number.

Constraints

1 <= input <=9
 Sample Input 1

5

Sample Output 1

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

Code:

#include <stdio.h> int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { printf("*"); } int s = 2 * n - 2 * i; for (int j = 1; j <= s; j++) { printf(" "); } for (int k = 1; k <= i; k++) { printf("*"); } printf("\n"); } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= i; j++) { printf("*"); } int s = 2 * n - 2 * i; for (int j = 1; j <= s; j++) { printf(" "); } for (int k = 1; k <= i; k++) { printf("*"); } printf("\n"); } return 0; }
Code language: C/AL (cal)

Leave a Comment