C program to Print Alphabet R star pattern
Example
#include <stdio.h>
int main()
{
int rows;
printf("Enter Rows = ");
scanf("%d", &rows);
for (int a = 0; a < rows; a++)
{
for (int b = 0; b < rows; b++)
{
if ((a == 0 || a == rows / 2) && (0 < b < rows - 1))
{
printf("*");
}
else if ((a != 0) && (b == 0 || (b == rows - 1 && a < rows / 2 )))
{
printf("*");
}
else if(b == a && a >= rows / 2)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
This C Program Print a pattern in the shape of the capital letter R using asterisks (*)
Explain Program
Step :1
#include <stdio.h>
Includes the standard input/output library for functions like printf() and scanf().
Step :2
int main() {
Start of the main function where the execution begins.
Step :3
int rows;
printf("Enter Rows = ");
scanf("%d", &rows);
rows: User-defined number of rows for the pattern.
scanf reads the number from the user.
Step :4
for (int a
= 0; a < rows; a++)
Iterates over each row (a from 0 to rows - 1).
Step :5
for (int b = 0; b < rows; b++)
For each row, prints characters in the columns (b from 0 to rows - 1).
Step :6
if ((a == 0 || a == rows / 2) && (0 < b < rows - 1))
When it's the first row (a == 0) or the middle row (a == rows / 2)
AND column is not at the edges
Then print * to form the top and middle horizontal bars of the 'R'
Step :7
Not the first row (a != 0)
AND
First column (b == 0) → draws the left vertical line of 'R'
OR: Last column (b == rows - 1) AND row is in top half → forms top-right vertical for the
top semi-circle of 'R'
Step :8
else if (b
== a && a >= rows / 2)
- For rows in the bottom half (a
>= rows / 2)
- And when b == a →
diagonal
- This draws the slanting leg of 'R' from the middle down to the bottom-right.
Step :9
else {
printf(" ");
}
- If none of the above conditions are met, print space.
Step :10
printf("\n");
- Moves to the next line after finishing a row.
return 0;
Program ends
Star pattern C program to Print Alphabet R
Example
No comments:
Post a Comment