w3resource

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:

Java: Print an American flag on the screen

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:

Flowchart: Java exercises: Print an American flag on the screen

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:

Flowchart: Java exercises: Print an American flag on the screen.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/basic/java-basic-exercise-14.php