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                                                                 
                                                                                                              
    *                                                                                                         
   ***                                                                                                        
  *****                                                                                                       
 *******                                                                                                      
*********                                                                                                     
 *******                                                                                                      
  *****                                                                                                       
   ***                                                                                                        
    *

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.