patterns in c programing

 


Pattern 1: Square of Asterisks

c
#include <stdio.h> int main() { int rows, cols, i, j; printf("Enter number of rows and columns: "); scanf("%d %d", &rows, &cols); for (i = 1; i <= rows; i++) { for (j = 1; j <= cols; j++) { printf("* "); } printf("\n"); } return 0; }
 

Output for Pattern 1:

For input rows = 4 and columns = 4:

markdown
* * * * * * * * * * * * * * * *

Pattern 2: Right Triangle of Asterisks

c
#include <stdio.h> int main() { int rows, i, j; printf("Enter number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; }

Output for Pattern 2:

For input rows = 5:

markdown
* * * * * * * * * * * * * * *

Pattern 3: Inverted Right Triangle of Asterisks

c
#include <stdio.h> int main() { int rows, i, j; printf("Enter number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; }

Output for Pattern 3:

For input rows = 4:

markdown
* * * * * * * * * *


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.