C program to print a Plus Star Pattern – In this article, we will discuss the numerous methods to print a Plus Star Pattern in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The methods used in this piece are as follows:
- Using For Loop
- Using While Loop
- Using Do-While Loop
It is a pretty common thing to say that Plus is one of the most used symbols of all time. It forms the base of mathematics, being one of the heavily used operators.
A plus sign is also denoted for Medical purposes. It is like the trademark symbol.
As you can see in the image uploaded above, you need to enter the number of rows accordingly.
The number of rows entered for the Plus Pattern here is 5.
Then, select the symbol to print the Plus Pattern.
Funnily enough, the ‘+’ symbol is chosen to print the Plus Pattern, making it a Plus-ception.
You can see that the pattern gets printed as per the code successfully.
Thus, the methods used to do so in C programming are as follows:
Plus Star Pattern – Using For Loop
- Read the entered number of rows, entered characters and store the number into the variable n, character into the variable ch.
2) The 1st for loop iterates through rows with the structure for(i=1;i<=n*2-1;i++).
3) If i!=number of rows
a) The 1st inner for loop iterates through columns with the structure for(j=1;j<=n;j++).
If j==n it prints symbol.Other wise it prints space.
4) At i=nth row
a) The 2nd inner for loop iterates through columns with the structure for(j=1;j<=n*2-1;j++).
It prints symbol.
5) The cursor comes to the next line after each row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); for(int i=1;i<=n*2-1;i++) { if(i!=n) for(int j=1;j<=n;j++) { if(j==n) printf("%c",ch); printf(" "); } else for(int j=1;j<=n*2-1;j++) { printf("%c",ch); } printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of rows: 5 Enter the symbol: + + + + + +++++++++ + + + + |
Using While Loop
- The while loop is entry checking loop. It checks the condition first for each value of i. If it is true then only it executes the code.
2) i=1, The outer while loop with the condition i<=n*2-1 iterates through rows until the condition becomes false.
3) if i!=n
a) j=1, The 1st inner while loop iterates through columns until the condition j<=n becomes false.
It prints symbol if j=n, otherwise it prints space.
j value increased by 1(go to while loop).
4) At i=nth row
a) j=1, The 2nd inner while loop iterates through columns until the condition j<=n*2-1 becomes false.
It prints symbol.j value increased by 1(go to step a).
5) Cursor comes to next line.i value increased by 1(go to outer while loop).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <conio.h> #include<stdio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); i=1; while(i<=n*2-1) { if(i!=n) { j=1; while(j<=n) { if(j==n) printf("%c",ch); printf(" "); j++ ; } } else { j=1; while(j<=n*2-1) { printf("%c",ch); j++; } } printf("\n"); i++; } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of rows: 5 Enter the symbol: . . . . . ......... . . . . |
Using Do-While Loop
- i=1, The outer do-while loop iterates through rows.
2) If i!=n
a) The 1st inner do-while loop iterates through columns.
j=1, It prints symbol if j=n, otherwise it prints space.
j value increased by 1.Checks the condition j<=n. If the condition is true then the inner loop iterates again. Repeats until the condition becomes false.
3) At i=nth row
a) j=1. the 2nd inner do-while loop iterates through columns.
Prints symbol.j value increased by 1.
Checks the condition j<=n*2-1. If the condition is true then the 2nd inner loop iterates again. Repeats until the condition becomes false.
4) Cursor comes to next line. i value increased by 1. Checks the condition i<=n*2-1. If the condition is true then the outer loop iterates again. Repeats until the condition becomes false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); i=1; do { if(i!=n) { j=1; do { if(j==n) printf("%c",ch); printf(" "); j++ ; }while(j<=n); } else { j=1; do { printf("%c",ch); j++; }while(j<=n*2-1); } printf("\n"); i++; } while(i<=n*2-1); return 0; } |