C language - Patterns Display - 012 (Hour Glass) #coding #patterncoding #programming #education

preview_player
Показать описание
Building Hour glass 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 lesser than and greater than symbols respectively)

#include <stdio.h>

int main() {
int n;
printf("Enter size: ");
scanf("%d",&n);

// Top half
for (int i = 0; i < n; i++) {
// Leading spaces
for (int j = 0; j < i; j++) {
printf(" ");
}
// Stars
for (int k = 0; k < (2 * (n - i) - 1); k++) {
printf("*");
}
printf("\n");
}

// Bottom half
for (int i = 1; i < n; i++) {
// Leading spaces
for (int j = 0; j < n - i - 1; j++) {
printf(" ");
}
// Stars
for (int k = 0; k < (2 * i + 1); k++) {
printf("*");
}
printf("\n");
}

return 0;
}

Output:

Enter size: 10
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
Рекомендации по теме
welcome to shbcf.ru