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.
void main()
- The
program execution starts
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).
k: used to control the number of asterisks (*)
printed in each row
printf("enter the number");
scanf("%d", &r);
- enter how many rows they want in the pyramid.
for(i = 1; i <= r; i++, k = 0)
- iteration correspond to one row of the pyramid.
- k =
0 resets the asterisk counter for each row.
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.
. 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.
printf("\n");
- After
printing spaces and stars for a row, it moves the cursor to the next line.
C Program to Print Full Pyramid of star pattern
Program Output :-
Related Post :-
Comments
Post a Comment