Hello there! 
Are you ready to dive into the exciting world of printing patterns in the C programming language? 
Patterns are a fascinating way to explore the power of programming and unleash your creativity while learning essential coding skills. 



In this lesson, we will embark on a journey to learn how to create stunning patterns using loops, conditionals, and string manipulation in C. Whether you're a beginner or have some prior programming experience, this lesson will provide you with the tools and techniques to master the art of printing patterns. 

  • Code Snippet:

    #include <stdio.h>
    int main ()
    {
        int n = 5;
        //First outer loop to iterate through each row
        for (int i = 0; i < 2 * n - 1; i++) {
            // assigning values to the comparator according to
            // the row number
            int comp;
            if (i < n) {
                comp = 2 * (n - i) - 1;
            }
            else {
                comp = 2 * (i - n + 1) + 1;
            }
            // first inner loop to print leading whitespaces
            for (int j = 0; j < comp; j++) {
                printf(" ");
            }
            // second inner loop to print stars *
            for (int k = 0; k < 2 * n - comp; k++) {
                printf("* ");
            }
            printf("\n");
        }
        return 0;
    }


  • Code Analysis: The given code is written in C and prints a pattern of asterisks (*) in the form of a diamond shape. Let's go through it step by step to understand how it works. 
    1. The code includes the standard input/output (stdio.h) header file, which provides functions for input and output operations, such as print. 
    2. The main() function is the entry point of the program and serves as the starting point of execution. 
    3. The integer variable 'n' is initialized with the value 5. This variable represents the number of rows in the diamond pattern and determines the size of the pattern. 
    4. The outer 'for' loop iterates through each row of the diamond pattern. It uses the loop variable 'i' to keep track of the current row, starting from 0 and going up to 2*n-1
    5. The first inner for loop, nested inside the outer loop, prints the leading whitespaces. It uses the loop variable 'j' to print the 'comp' number of spaces using the printf() function. 
    6. The second inner for loop, also nested inside the outer loop, prints the asterisks (*) using the printf() function. The number of asterisks printed in each row is calculated as 2*n - comp, which is the total width of the row. 
    7. After printing the leading whitespaces and asterisks for each row, a newline character '\n' is printed using printf() to move to the next row. 
    8. The outer loop continues to iterate through each row until all rows are printed, completing the diamond pattern. 
    9. Finally, the main() function returns 0, indicating the successful completion of the program. 
    10. The given C program prints a diamond pattern using asterisks (*) as the pattern character. The size of the diamond pattern is determined by the variable 'n' which is initialized to 5. 

  • Code Output: 
PS G:\code\> cd "g:\code\" ; if ($?) { gcc a.c -o a } ; if ($?) { .\a }

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

The diamond pattern is formed by printing spaces before and after the asterisks in each row. The number of spaces before and after the asterisks in each row is determined by the value of the variable "comp" which is calculated based on the row number using the conditional statement inside the outer loop. The first half of the diamond is printed in the upper half of the pattern, and the second half of the diamond is printed in the lower half of the pattern. The diamond shape is created by adjusting the number of spaces before and after the asterisks in each row, gradually increasing up to the middle row, and then decreasing in the lower half of the pattern.