Write a C Program to Print Plus Star Pattern using for loop. This c example uses if-else to check for the row and column values and prints the plus pattern.
#include<stdio.h> int main() { int i, j, rows; printf("Enter Plus Pattern Rows = "); scanf("%d", &rows); printf("Plus Star Pattern\n"); for(i = 1; i <= rows * 2 - 1; i++) { if(i != rows) { for(j = 1; j <= rows; j++) { if(j == rows) { printf("*"); } printf(" "); } } else { for(j = 1; j <= rows * 2 - 1; j++) { printf("*"); } } printf("\n"); } return 0; }
This C Program allows entering symbols to Print as the Plus Pattern. Here, we altered the logic to remove multiple if conditions.
#include<stdio.h> int main() { int i, j, rows; char ch; printf("Enter Symbol for Plus Pattern = "); scanf("%c", &ch); printf("Enter Plus Pattern Rows = "); scanf("%d", &rows); printf("Plus Star Pattern\n"); for(i = 1; i <= rows; i++) { if(i == ((rows / 2) + 1)) { for(j = 1; j <= rows; j++) { printf("%c", ch); } } else { for(j = 1; j <= rows/2; j++) { printf(" "); } printf("%c", ch); } printf("\n"); } return 0; }
Enter Symbol for Plus Pattern = $
Enter Plus Pattern Rows = 10
Plus Star Pattern
$
$
$
$
$
$$$$$$$$$$
$
$
$
$
This C program is the same as the first example. However, this C example uses a while loop to print Plus Star Pattern.
#include<stdio.h> int main() { int i, j, rows; printf("Enter Plus Pattern Rows = "); scanf("%d", &rows); printf("Plus Star Pattern\n"); i = 1; while(i <= rows * 2 - 1) { if(i != rows) { j = 1; while(j <= rows) { if(j == rows) { printf("*"); } printf(" "); j++; } } else { j = 1; while(j <= rows * 2 - 1) { printf("*"); j++; } } printf("\n"); i++; } return 0; }
Enter Plus Pattern Rows = 6
Plus Star Pattern
*
*
*
*
*
***********
*
*
*
*
*