Mastering Pattern Printing in C Programming | Lecture 08


Understanding Nested Loops

Nested loops are a powerful tool in programming, allowing you to perform complex tasks with ease. When you place one loop inside another, the inner loop runs completely every time the outer loop runs once. This is particularly useful for working with multi-dimensional data, such as printing patterns, working with matrices, or performing repeated actions in a structured way.

Key Points to Remember:
  • Outer Loop: Controls the number of iterations of the overall process (e.g., the number of rows).
  • Inner Loop: Executes its statements for each iteration of the outer loop (e.g., the number of columns or elements in a row).
  • Execution Flow: The inner loop runs to completion every time the outer loop runs once.

Pattern Printing Examples

Example 1: Simple Number Pattern

1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 

Explanation: For this pattern, you need two while loops: one for the row and another for the column. You simply print the column number in each iteration.

int row=1, col;
while(row<=5)
{
col=1;
while(col<=5)
{
printf("%d",col);
col++;
}
printf("\n");
row++;
}

Example 2: Row-Based Number Pattern

11111
22222
33333
44444
55555

Explanation: The logic is similar to the first example, but instead of printing the column number, you print the row number.

int row=1, col;
while(row<=5)
{
col=1;
while(col<=5)
{
printf("%d",row);
col++;
}
printf("\n");
row++;
}

Example 3: Increasing Number Pattern

1
12
123
1234
12345

Explanation: The number of columns printed increases with each row. The outer loop controls the row, and the inner loop controls the number of columns printed per row.

int row=1, col;
while(row<=5)
{
col=1;
while(col<=row)
{
printf("%d",col);
col++;
}
printf("\n");
row++;
}

Example 4: Decreasing Number Pattern

54321
4321
321
21
1

Explanation: This pattern decreases the number of columns printed with each row, starting from the highest number and decrementing both the row and column count.

int row=5, col;
while(row>=1)
{
col=1;
while(col<=row)
{
printf("%d",col);
col++;
}
printf("\n");
row--;
}

Practice Questions: Sharpen Your Skills

Below are some practice questions to help you solidify your understanding of nested loops and pattern printing. Try to solve these on your own, and share your solutions in the comments section!

Question 1: Triangle Star Pattern

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

Hint: Use a nested loop where the outer loop runs from 1 to 5 (for the rows), and the inner loop runs from 1 to the current row number.

Question 2: Inverted Number Pattern

54321
4321
321
21
1

Hint: The outer loop runs from 5 to 1, and the inner loop prints numbers starting from the outer loop's value down to 1.

Question 3: Pyramid Star Pattern

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

Hint: You’ll need two nested loops, one for printing spaces and one for stars, with the outer loop controlling the number of rows.

I encourage you to try these practice questions and share your code in the comments section below. Let’s learn and grow together!

    Thank you for reading! Your thoughts and suggestions are always welcome—let’s connect in the comments below!

0 Comments

Types of Arrays in C Programming | Lecture 10

In C programming, arrays are a fundamental concept that allows you to store multiple values of the same data type in a single variable. Instead of using multiple variables to store similar data, arrays make it easier to manage and manipulate these values. Types of Arrays There are two main types of arrays in C: One-Dimensional Arrays Multi-Dimensional Arrays One-Dimensional Arrays A one-dimensional array is like a list where all the elements are stored in a single row or column. It uses only one subscript to access each element. Declaration: DataType variable_name[size]; Here, DataType is the type of elements (e.g., int , float ), variable_name is the name of the array, and size is the number of elements the array can hold. Example: Declaring an integer array with 5 elements: int number[5]; This array can store 5 integers, with indices ranging from 0 to 4. Initialization of One-Dimensional Arrays Arrays can be initialized at the time of...