Printing Star Patterns Using Do-While Loops in C : Techniques and Examples
Printing star patterns is a common exercise in programming that helps to understand control structures and loops in C . In this article, we will explore how to use the do-while loop to print various star patterns. We will start with a simple example and then move on to more complex patterns. By the end of this guide, you will have a comprehensive understanding of how to print star patterns using do-while loops.
Introduction to Do-While Loops
A do-while loop in C is a control flow statement that allows you to execute a block of code at least once, regardless of the condition. This makes it particularly useful when you need to ensure that a block of code runs at least once before checking the condition.
Example: Right-Angled Triangle Star Pattern
Here is a simple example of using the do-while loop to print a right-angled triangle star pattern. The program prompts the user to enter the number of rows and then prints the corresponding star pattern.
#include iostream using namespace std; int main() { int rows; cout "Enter the number of rows: "; cin rows; int i 1; do { int j 1; do { cout "*"; j ; } while (j i); cout endl; // Move to the next line after each row i ; } while (i rows); return 0; }
In this example:
The outer do-while loop controls the number of rows. The inner do-while loop prints a number of stars corresponding to the current row number. The condition for the inner loop ensures that the number of stars printed matches the row number. The condition for the outer loop ensures that the pattern continues until the specified number of rows is printed.Sample Output
If the user inputs 5, the output will look like this:
* ** *** **** *****
Creating Custom Star Patterns
The example provided above is a straightforward right-angled triangle. However, with a bit of creativity, you can create a wide variety of star patterns. Here are a few more examples to get you started:
Example: Inverted Right-Angled Triangle Star Pattern
To create an inverted right-angled triangle, you need to change the loop conditions slightly. Here is the code:
#include iostream using namespace std; int main() { int rows; cout "Enter the number of rows: "; cin rows; int i rows; do { int j 1; do { cout "*"; j ; } while (j i); cout endl; // Move to the next line after each row i--; } while (i 1); return 0; }
Example: Hollow Right-Angled Triangle Star Pattern
To create a hollow right-angled triangle, modify the inner loop to ensure that only one star is printed per row:
#include iostream using namespace std; int main() { int rows; cout "Enter the number of rows: "; cin rows; int i 1; do { int j 1; do { if (i j || j 1 || i rows) { cout "*"; } else { cout " "; } j ; } while (j i); cout endl; // Move to the next line after each row i ; } while (i rows); return 0; }
Example: Square Star Pattern
To create a square star pattern, you can use two nested do-while loops:
#include iostream using namespace std; int main() { int rows; cout "Enter the number of rows: "; cin rows; int i 1; do { int j 1; do { cout "*"; j ; } while (j rows); cout endl; // Move to the next line after each row i ; } while (i rows); return 0; }
Conclusion
Printing star patterns using do-while loops is a versatile technique that can be applied to a wide range of patterns. By understanding the conditions and how to manipulate loops, you can create a variety of interesting and complex patterns. Whether you want to create simple or intricate star patterns, the do-while loop is a powerful tool in your programming arsenal.
If you have any further questions or need more examples, feel free to ask in the comments. Happy coding!