C Programming Lab Manual – 32

Write a C program to display a pattern like a diamond.

Function Main
    Declare Integer i, j, r
    Output "Input number of rows : "
    Input r
    For i = 0 to r
        For j = 1 to r-i
            Output " "
        End
        For j = 1 to 2*i-1
            Output "*"
        End
        Output ""
    End
    For i = r-1 to 1 decreasing
        For j = 1 to r-i
            Output " "
        End
        For j = 1 to 2*i-1
            Output "*"
        End
        Output ""
    End
End

#include <stdio.h>

int main()
{
   int i,j,r;
   printf("Input number of rows: ");
   scanf("%d",&r);
   for(i=0;i<=r;i++)
   {
     for(j=1;j<=r-i;j++)
     printf(" ");
     for(j=1;j<=2*i-1;j++)
       printf("*");
     printf("\n");
   }
 
   for(i=r-1;i>=1;i--)
   {
     for(j=1;j<=r-i;j++)
     printf(" ");
     for(j=1;j<=2*i-1;j++)
       printf("*");
     printf("\n");
   }
 return 0;
}

Input number of rows: 5                                                                 
                                                                                                              
    *                                                                                                         
   ***                                                                                                        
  *****                                                                                                       
 *******                                                                                                      
*********                                                                                                     
 *******                                                                                                      
  *****                                                                                                       
   ***                                                                                                        
    *

C Programming Lab Manual – 31

Write a C program to make such a pattern as a pyramid with an asterisk.

Function Main
    Declare Integer i, j, space, rows, k
    Output "Input number of rows : "
    Input rows
    Assign space = rows+4-1
    For i = 1 to rows
        For k = space to 1 decreasing
            Output " "
        End
        For j = 1 to i
            Output "* "
        End
        Output ""
        Assign space = space -1
    End
End

#include <stdio.h>
int main()
{
   int i,j,space,rows,k;
   printf("Input number of rows : ");
   scanf("%d",&rows);
   space=rows+4-1;
   for(i=1;i<=rows;i++)
   {
         for(k=space;k>=1;k--)
            {
              printf(" ");
            }
                      
	   for(j=1;j<=i;j++)
	   printf("* ");
	printf("\n");
    space--;
   }
 return 0;
}

Input number of rows  : 4                                                                                     
       *                                                                                                      
      * *                                                                                                     
     * * *                                                                                                    
    * * * *