Home >Backend Development >C#.Net Tutorial >How to display patterns in c language programming
In C language, patterns can be displayed by using loops and characters. The steps include: using a loop and the printf() function to create the lines. Create new lines with newlines. Nest loops to create more complex patterns, such as triangles.
Displaying patterns in C language programming
How to display patterns using C language programming?
In C language, you can create simple patterns by using characters and line breaks.
Steps:
1. Use the loop output to create rows:
<code class="c">for (int i = 0; i < n; i++) { printf("* "); }</code>
Where:
i
is a loop variable indicating the number of rows. n
is the number of lines to print. printf("* ")
Prints an asterisk (*) and a space. 2. Create new lines using newlines:
<code class="c">printf("\n");</code>
This will print a newline after each line, splitting the pattern into multiple lines.
3. Nested loops to create more complex patterns:
<code class="c">for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("* "); } printf("\n"); }</code>
This will create a right-aligned triangle pattern.
Example:
The following code will display a right-aligned triangle pattern:
<code class="c">#include <stdio.h> int main() { int n = 5; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("* "); } printf("\n"); } return 0; }</code>
Output:
<code>* * * * * * * * * * * * * * *</code>
The above is the detailed content of How to display patterns in c language programming. For more information, please follow other related articles on the PHP Chinese website!