C Program to print Hollow Full pyramid star Pattern
C Program to print Hollow Full pyramid star Pattern
Example :-
#include<stdio.h>
int main()
{
int i, space, row, star=0;
printf("Enter The Number ");
scanf("%d",&row);
for(i = 0; i < row-1; i++) {
for(space = 1; space < row-i; space++) {
printf(" ");
}
for (star = 0; star <= 2*i; star++) {
if(star==0 || star==2*i)
printf("*");
else
printf(" ");
}
printf("\n");
}
for(i=0; i<2*row-1; i++){
printf("*");
}
return 0;
}
C Program to Print Hollow Full Pyramid Using Stars Pattern
Explain Program
Step :1
#include <stdio.h>
int main()
{
int i, space, row,
star = 0;
#include <stdio.h> :- printf and scanf.
int i, space, row, star;: Declares loop variables and input
variable.
row = number of levels of the pyramid.
i = controls outer loop (rows).
space = manages indentation before the stars.
star = controls how many positions are printed per row.
Step :2
User Input
printf("Enter The Number ");
scanf("%d", &row);
User input how many rows they want for the pyramid.
If input is 5, the pyramid
Step :3
Hollow Pyramid
for(i = 0; i < row - 1; i++) {
for(space = 1;
space < row - i; space++) {
printf("
");
}
for(star = 0; star
<= 2 * i; star++) {
if(star == 0
|| star == 2 * i)
printf("*");
else
printf(" ");
}
printf("\n");
}
How it work:
- Loop 1) for (i = 0; i < row -
1; i++)
- Controls each row of the hollow pyramid .
- Runs row - 1 times (e.g., if row = 5, this loop runs 4
times).
- Loop 2) for (space = 1; space
< row - i; space++)
- Adds spaces to align the stars in a pyramid shape.
for(star = 0; star <= 2*i; star++)
Handle the character in the current row.
Always print * at the start (0) and end (2*i).
In between, print a space to make it hollow.
Step :5
Bottom Part:
for(i = 0; i < 2 * row - 1; i++) {
printf("*");
}
loop prints a full base of stars at the bottom.
C Program to print Hollow Full pyramid star Pattern
Output :-
Related Post :-
C program t -prin rhombus star pattern
c program to print floyds triangle
c program to print pascals triangle
Comments
Post a Comment