Diamond Pattern Programs In C

Problem Definition: 

Diamond pattern programs in C require repetition of an alphabet. Desired alphabet can be a character or a special symbol or a digit. In C language, repetition is achieved using loops. Thus, to develop the logic of the program, the learner must have the ability to set limits of the loops and must have the understanding of the relationship between outer and inner loop.

Desired Output: 

    *
  * * *
* * * * * 
  * * *
    *

Problem Analysis:

Desired output  can be achieved by breaking down into sub-parts as follows:

Learning to code for single asterisks *.           [Output : * ]
Learning to code for 3 asterisks ‘ * ‘ .               [Output: * * *]
Learning to code for 5 asterisks ‘ * ’.                [Output: * * * * * ]
Learning to code to obtain following output:
    
    * * * *
      * *
       *

Coding and Documentation:

We will start coding by taking one problem at a time as discussed in Problem analysis.

Code to print single asterisks ‘ * ’.
Code Block 1:

    
	# include <stdio.h>
          int main ( )
         {
                   int i;
                   for ( i = 1 ; i <= 1 ; i++)
                                 { 
                                     printf ( “ * ” );    
                                  }
                               return 0; 
         }
Output:
			       *

Code Analysis: 

In Code Block 1, execution begins from the “main ()” function. Variable i is declared as an integer variable. In the “for” loop variable i is initialized to value 1, terminating condition is set as “i<=1” it means that variable i cannot have value greater than 1. Value of the variable is incremented each time the “for” loop is executed and checked for terminating condition “i<=1” if it becomes true the loop is executed otherwise terminated.     

  • #include : It is a c language grammar to direct the compiler to include a file for standard input/output. This grammar is termed as Preprocessor Directory.  is header file having code for standard input/output. Angular brackets < > delimit the name of the header file, it may be replaced by double quotes “ ”. Hence, may also be written as  “stdio.h”. 
  • “Std” in <stdio.h> stands for standard console (may be considered as monitor), “i” in <stdio.h> stands for input and corresponds to scanf ( ) function used to take input from the user. “O” in <stdio.h> stands for output corresponding to the printf ( ) function used to print desired output on the monitor.
  • int main ( ) is the function from where code compilation will begin. “int” is a keyword. In ‘c’ language the definition of keywords can not be changed, “int” is defined as integer and has been allocated 4 bytes of memory to store value. It is also termed as a datatype. Since “int” is coming before function main ( ) [int main ( )] it is called as return type and specifies the data type of the value returned by function.

“for” is a loop that directs the compiler to execute code for a specified number of times.

 Syntax of “for” loop is:
for (initialization; terminating condition; increment/decrement)
{    
    ----------  lines of code     ------------
}

initialization  specifies the starting value form where the loop will begin its execution. It will repeat the lines of code contained within opening braces “{” and closing braces  “}” until the terminating condition becomes false. Each time the line of code is executed, initialized value is incremented/decremented and then checked for terminating condition, if it becomes true then loop is executed otherwise it is terminated.

“return  0 ” returns value 0 to the operating system to signal successful execution of the program.

Code to print 3 asterisks ‘ * ’ on the same line.

Code Block 2:

# include
int main ( )
{
int i;

for ( i = 1 ; i &lt;= 3 ; i++)
{
printf ( " * ");
}
return 0;
}
Output:						 
 * * *

Code Analysis: 

In Code Block 2 the code within the “for” loop will be executed three times until the terminating condition “i<=3” becomes false.

  1. Code to print 5 asterisks ‘ * ’ on the same line.

Code Block 3:

# include <stdio.h>
          int main ( )
         {
                   int i;
                   
                    for ( i = 1 ; i <= 5 ; i++)
                                 { 
                                     printf ( " * " );    
                                  }
                               return 0; 
         }
Output:
		* * * * *

Code Analysis: 

In Code Block 3 programming instructions within the for loop will be executed 5 times until the terminating condition “i<=5” becomes false.

At present we are in a state to print asterisks ‘ * ’ desired number times, that is, one, three and five. But if we combine the “for” loop of Code Block 1, Code Block 2 and Code Block 3 into a single programming construct than Code Block 4 come-out as follows:

Code Block 4:


# include &lt;stdio.h&gt;
int main ( )
{
int i;

for ( i = 1 ; i &lt;= 1 ; i++)
{
printf ( " * " );
}
for ( i = 1 ; i &lt;= 3 ; i++)
{
printf ( " * " );
}

for ( i = 1 ; i &lt;= 5 ; i++)
{
printf ( " * " );
}
return 0;
}

Output:
		* * * * * * * * *

Code Analysis: 

In Code Block 4 we are getting a total of 8 asterisks and that too on the same line. But if we desire to print asterisks on separate lines than we have to code something like this: 

Code Block 5:

# include
int main ( )
{
int i;
for ( i = 1 ; i &lt;= 1 ; i++)
{
printf ( " * ");
printf("\n");
}
for ( i = 1 ; i &lt;= 3 ; i++);
{
printf ( " * ");
}
printf("\n");
for ( i = 1 ; i &lt;= 5 ; i++)
{
printf ( " * ");
}
printf("\n");
return 0;
}
Output:
	*
	* * *
	* * * * *

Code Analysis:

As you can see in the output we are coming closer to the desired output. In the output of Code Block 4 we are getting asterisks on three different lines, this became possible due to appropriate use of “\n”. “\n” is a new line character in ‘c’ language, it marks the beginning of the new line. printf(“\n”) will print output on a new line.

Required output is diamond shape and the output we are getting is the right angled triangle. So, we will have to place the asterisks in such a way that the Diamond pattern appears. In order to achieve this, code will be as follows:

Code Block 6:

# include <stdio.h>
                     int main ( )
                    {
                               int  i,  j, k;
                               int n=5;
                               for ( i=1; i <= n; i++)
                                 {
                                      for (j=0; j<n-i; j++)
                                       {
                                           printf(" ");
                                     }
                                      for (j=1; j<=i; j++)
                                      {
                                            printf("*");
                                       }
                                        for (k=1; k<i; k++)
                                       {
                                               printf("*");
                                        }
                                       printf("\n") ;
                                     }
                                return 0;
                      }

Output:
                   *
	         * * *
               * * * * *
              * * * * * * *
            * * * * * * * * *

In the output of code block 6 it can be observed that the first half of diamond has been achieved. In code analysis it can be observed that the programming construct has been written using nested “for”.

To get the desired output Diamond pattern programs in C, we have done code for the first half of the diamond, to get the second half of the diamond following will be the code: 

Code Block 7:

# include
int main ( )
{
int i, j, k;
int n=5;
for ( i=n-1; i &gt;= 1; i--)
{
for (j=0; j&lt;(n-i); j++)
{
printf(" ");
}
for (j=1; j&lt;=i; j++)
{
printf("*");
}
for (k=1; k&lt;i; k++)
{
printf("*");
}
printf("\n") ;
}
return 0;
}
Output:
*********
*******
*****
***
*

If Code Block 6 and Code Block 7 are combined then we will be able to obtain the desired output that is Diamond patterns programs in C.

Following code prints Diamond patterns programs in C.

Code Block 8:

 #include <stdio.h>
       int main( )
       { 
             int n, i, j, k;
             printf("Enter Number:");
             scanf("%d", &n);
             for(i=1; i<=n; i++)
             {
                   for(j=0; j<n-i; j++)
                   {
                          printf(" ");
                    }
                 for (j=1; j<=i; j++)
                   {
                        printf ("*");
                    }
                  for (k=1;k<i;k++)
                  {
                       printf("*");
                   } 
                       printf("\n");
             }
             for( i = n-1; i>=1; i--)
               {
                     for (j=0; j<n-i; j++)
                      {
                           printf(" ");
                       }
                       for(j=1; j<=i; j++)
                        {
                               printf("*");
                          }
                         for(k=1; k<i; k++)
                          {
                               printf("*");
                           }
                           printf("\n");
                        }
                      return 0;
                   }
Desired Output:

        *

      * * *

    * * * * *

  * * * * * * *

* * * * * * * * *

  * * * * * * *

    * * * * *

      * * *

        *