Assignment: Module 03- Part 02
Write a program FloydExample3 to display the output as below using control structures.
* * * *
* * *
* *
*
public class FloydExample3 {
public static void main(String[] args) {
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
The output of the above code will be:
* * * *
* * *
* *
* The above Java program demonstrates a simple yet important concept in programming: the use of nested loops and control structures to generate patterns. In this case, the pattern is an inverted right-angled triangle made up of asterisks (*). The outer loop controls the number of rows, while the inner loop determines how many stars are printed in each row. As the value of i decreases with each iteration of the outer loop, the number of stars printed also decreases, creating the desired pattern.
This type of problem is commonly used to strengthen logical thinking and improve understanding of loop behavior. When beginners start learning Java or any programming language, pattern-based questions like this help them visualize how loops interact with each other. The outer loop essentially sets the boundary for how many times the pattern will repeat vertically, and the inner loop handles the horizontal printing of characters. Together, they form a structured output that changes predictably with each iteration.
Another important aspect of this program is how clean and readable it is. The logic is straightforward, making it easy to understand and modify. For example, if you want to increase the size of the pattern, you can simply change the initial value of i in the outer loop. Similarly, if you want to print a different character instead of an asterisk, you can modify the print statement accordingly. This flexibility makes such programs a great starting point for experimenting with variations and enhancements.
You can also extend this program further by adding spaces before the stars to create a right-aligned or centered triangle. This would involve introducing another loop to print spaces before printing the stars in each row. By doing so, you move from a basic pattern to a more advanced one, which helps deepen your understanding of nested loops and formatting output in Java.
In real-world programming, the concepts used here are not limited to printing patterns. Nested loops are widely used in scenarios like processing matrices, handling multi-dimensional arrays, and working with complex data structures. Understanding how to control the flow of loops efficiently is a fundamental skill that applies to many areas of software development.
Additionally, practicing such programs helps improve problem-solving skills. It encourages you to think step by step, analyze how each loop contributes to the final output, and debug any issues that arise. Over time, this builds confidence and makes it easier to tackle more complex programming challenges.
Overall, this FloydExample3 program is a simple yet powerful example of how control structures work in Java. By mastering such basic programs, you lay a strong foundation for advanced programming concepts. Continuous practice with variations of pattern problems will not only enhance your coding skills but also improve your logical reasoning and ability to write efficient, structured code.

.jpg)