C Program
C Program to Print Full Pyramid of star pattern
Example
#include<stdio.h>
void main ()
{
int i,space,r,k=0;
printf("enter the number");
scanf("%d",&r);
for(i=1;i<=r;i++,k=0)
{
for(space=1;space<=r-i;space++)
{
printf(" ");
}
while(k !=2*i-1){
printf("*");
++k;
}
printf("\n");
}
return 0;
}
C Program to Print Full Pyramid of star pattern
Explain Program
Step :1
#include <stdio.h>
- Standard Input Output header file.
- using printf() and scanf() functions.
Step :2
void main()
- The program execution starts
Step :3
Variable Declarations
int i, space, r, k = 0;
- i: loop counter for rows.
- space: counter for printing leading spaces in each row.
- r: number of rows in the pyramid (user input).
Step :4
printf("enter the number");
scanf("%d", &r);
- enter how many rows they want in the pyramid.
Step :5
for(i = 1; i <= r; i++, k = 0)
- Runs from i = 1 to r
- iteration correspond to one row of the pyramid.
- k = 0 resets the asterisk counter for each row.
Step :6
Inner Loop 1: Printing Leading Spaces
for(space = 1; space <= r - i; space++)
{
printf("
");
}
- This prints leading spaces before the stars in each row.
- row number (i) increases, the number of spaces decreases.
- centers the pyramid.
Step :7
. Inner Loop 2: Printing Stars
while(k != 2 * i - 1)
{
printf("*");
++k;
}
- print the asterisks (*) in each row.
- number of stars increases by 2 with each row.
- The formula 2 * i - 1 gives an odd number of stars (1, 3, 5, ...), forming a full pyramid.
Step :8
printf("\n");
- After printing spaces and stars for a row, it moves the cursor to the next line.
Step :9
return 0;
No comments:
Post a Comment