Saturday, July 5, 2014

Simple Star Pattern Program

 Lets write a program to print this pattern.
 
    *
   ***
  *****
 *******
*********
This is a very basic program and it will test you understanding of loops. 
 
#include<stdio.h>

int main()

{

 int rows,i,j,k,l;

printf("enter the number of rows you would like to see in the pyramid \n");

scanf("%d",&rows);


for( i=0;i<rows;i++)    //print each row one by one

{

for( j=0;j<(rows-i);j++)   //to print initial blanks

{

printf(" ");

}

for( k=0;k<(2*i+1);k++)      // to print stars

{

printf("*");

}


printf("\n");
}

return 0;
}



NB: we don't need to print the later blank spaces as it will be there automatically when adjusted

No comments:

Post a Comment