Java: Print an American flag on the screen
Java Basic: Exercise-14 with Solution
Write a Java program to print an American flag on the screen.
Pictorial Presentation:

Sample Solution:
Java Code:
public class Exercise14 {
public static void main(String[] args) {
// Print a pattern of asterisks and equal signs to create a design
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");
System.out.println(" * * * * * ==================================");
System.out.println("* * * * * * ==================================");
// Print a row of equal signs to complete the design
System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
System.out.println("==============================================");
}
}
Sample Output:
* * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== ============================================== ============================================== ============================================== ============================================== ============================================== ==============================================
Flowchart:

Sample Solution:
Java Code:
public class Main {
public static void main(String[] args) {
// Define pattern strings for the top and middle sections
String p1 = "* * * * * * ==================================\n * * * * * ==================================";
String p2 = "==============================================";
// Print the top section pattern 4 times
for (int i = 0; i < 4; i++) {
System.out.println(p1);
}
// Print the bottom section pattern once
System.out.println("* * * * * * ==================================");
// Print the middle section pattern 6 times
for (int i = 0; i < 6; i++) {
System.out.println(p2);
}
}
}
Output:
* * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== ============================================== ============================================== ============================================== ============================================== ============================================== ==============================================
Flowchart:

Java Code Editor:
Previous: Write a Java program to print the area and perimeter of a rectangle.
Next: Write a Java program to swap two variables.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join