Saturday, July 5, 2014

Inverted Pyramid of Characters

Let's try to write a program which will print an inverted pyramid of character like this if we give input as 4

ABCDCBA
   ABCBA
      ABA
         A

#include<stdio.h>

int main()

{

 int rows,i,j,k;

printf("enter the number of rows you want to print \n");
scanf("%d",&rows);

for(i=0;i<rows;i++)
{
  
  
    for(j=0;j<i;j++)
    {
        printf(" ");
    }
  
    for(k=0;k<((rows-1)-i);k++)
    {
        printf("%c ",(65+k));
    }
  
    for(;k>=0;k--)
    {
    printf("%c ",(65+k));
    }
    printf("\n");
}


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