C Online Compiler
Example: Butterfly Pattern Printer in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Butterfly Pattern Printer #include <stdio.h> int main() { int n, i, j; // Step 1: Prompt user for input printf("Enter the number of rows for one half of the butterfly (e.g., 4 for a medium size): "); scanf("%d", &n); // Step 2: Print the upper half of the butterfly // This loop iterates from row 1 to 'n' (the widest point) for (i = 1; i <= n; i++) { // Left wing stars: Prints 'i' stars, increasing with each row for (j = 1; j <= i; j++) { printf("*"); } // Spaces in between: Prints 2 * (n - i) spaces, decreasing with each row for (j = 1; j <= 2 * (n - i); j++) { printf(" "); } // Right wing stars: Prints 'i' stars, increasing with each row for (j = 1; j <= i; j++) { printf("*"); } printf("\n"); // Move to the next line after each row } // Step 3: Print the lower half of the butterfly // This loop iterates from row 'n-1' down to 1 (mirroring the upper half) for (i = n - 1; i >= 1; i--) { // Left wing stars: Prints 'i' stars, decreasing with each row for (j = 1; j <= i; j++) { printf("*"); } // Spaces in between: Prints 2 * (n - i) spaces, increasing with each row for (j = 1; j <= 2 * (n - i); j++) { printf(" "); } // Right wing stars: Prints 'i' stars, decreasing with each row for (j = 1; j <= i; j++) { printf("*"); } printf("\n"); // Move to the next line after each row } return 0; }
Output
Clear
ADVERTISEMENTS