write a program in c that print letter s which is 15 by 18 rows and column big with…
- User
write a program in c that print letter s which is 15 by 18 rows and column big with astherik using for and if statement
- Assistant
write a program in c that print letter s which is 15 by 18 rows and column big with astherik using for and if statement
Certainly! Below is a C program that prints the letter 'S' using asterisks (*). The pattern is designed to be 15 columns wide and 18 rows tall. The program uses nested for loops and if statements to determine where to place the asterisks to form the shape of the letter 'S'.
#include <stdio.h>
int main() {
int rows = 18; // Total number of rows
int cols = 15; // Total number of columns
// Iterate over each row
for(int i = 0; i < rows; i++) {
// Iterate over each column in the current row
for(int j = 0; j < cols; j++) {
// Conditions to print '*' for letter 'S'
// Top horizontal line
if(i == 0) {
printf("*");
}
// Middle horizontal line
else if(i == rows / 2) {
printf("*");
}
// Bottom horizontal line
else if(i == rows - 1) {
printf("*");
}
// Left vertical line (upper part)
else if(j == 0 && i < rows / 2) {
printf("*");
}
// Right vertical line (lower part)
else if(j == cols - 1 && i > rows / 2) {
printf("*");
}
// Fill the spaces inside the 'S' shape
else {
printf(" ");
}
}
// Move to the next line after completing a row
printf("\n");
}
return 0;
}
Variables:
rows: Represents the total number of rows (18) for the pattern.cols: Represents the total number of columns (15) for the pattern.Outer Loop (for(int i = 0; i < rows; i++)):
0 to 17.Inner Loop (for(int j = 0; j < cols; j++)):
0 to 14 within the current row.Conditions to Print *:
i == 0):
* across the entire first row.i == rows / 2):
* across the middle row to create the horizontal bar of 'S'.i == rows - 1):
* across the last row.j == 0 && i < rows / 2):
* on the first column for the upper half to form the top curve of 'S'.j == cols - 1 && i > rows / 2):
* on the last column for the lower half to form the bottom curve of 'S'.else):
" " for all other positions not part of the 'S' shape.Printing New Line (printf("\n")):
When you run the program, it will display a large 'S' made of asterisks with the specified dimensions. Here's how the output will look:
***************
*
*
*
*
*
*
*
*
***************
*
*
*
*
*
*
*
***************
Save the Code:
print_S.c.Compile the Code:
print_S.c is saved.gcc:
gcc -o print_S print_S.c
Run the Executable:
./print_S
Feel free to adjust the rows and cols variables to modify the size of the letter 'S' as per your requirements.
Not yet included in a published release.