filmov
tv
C language - Patterns Display - 005 #coding #correctcoding #programming #education

Показать описание
Building below pattern according to the given input size in c language.
Helps for people who just entered into the programming world and needing to improve their logical knowledge.
Code: (Before execution, replace < and > with lessthan and greaterthan symbols respectively)
#include <stdio.h>
int main() {
int i, j, space;
int rows;
printf("Enter halfth Size:");
scanf("%d", &rows);
// Top half (including middle)
for (i = 0; i < rows; i++) {
// Left stars
for (j = 1; j <= rows - i; j++) {
printf("*");
}
// Middle spaces
for (space = 0; space < 2 * i; space++) {
printf(" ");
}
// Right stars
for (j = 1; j <= rows - i; j++) {
printf("*");
}
printf("\n");
}
// Bottom half (mirror of the top, excluding middle row)
for (i = 0; i < rows; i++) {
// Left stars
for (j = 0; j <= i; j++) {
printf("*");
}
// Middle spaces
for (space = 0; space < 2 * (rows - i - 1); space++) {
printf(" ");
}
// Right stars
for (j = 0; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output:
Enter halfth Size:10
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********************
Helps for people who just entered into the programming world and needing to improve their logical knowledge.
Code: (Before execution, replace < and > with lessthan and greaterthan symbols respectively)
#include <stdio.h>
int main() {
int i, j, space;
int rows;
printf("Enter halfth Size:");
scanf("%d", &rows);
// Top half (including middle)
for (i = 0; i < rows; i++) {
// Left stars
for (j = 1; j <= rows - i; j++) {
printf("*");
}
// Middle spaces
for (space = 0; space < 2 * i; space++) {
printf(" ");
}
// Right stars
for (j = 1; j <= rows - i; j++) {
printf("*");
}
printf("\n");
}
// Bottom half (mirror of the top, excluding middle row)
for (i = 0; i < rows; i++) {
// Left stars
for (j = 0; j <= i; j++) {
printf("*");
}
// Middle spaces
for (space = 0; space < 2 * (rows - i - 1); space++) {
printf(" ");
}
// Right stars
for (j = 0; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output:
Enter halfth Size:10
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********************
Комментарии