Java Exercises: 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)
{
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("* * * * * * ==================================");
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) {
String p1 = "* * * * * * ==================================\n * * * * * ==================================";
String p2 = "==============================================";
for (int i = 0; i < 4; i++) {
System.out.println(p1);
}
System.out.println("* * * * * * ==================================");
for (int i = 0; i < 6; i++) {
System.out.println(p2);
}
}
}
Output:
* * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== * * * * * ================================== * * * * * * ================================== ============================================== ============================================== ============================================== ============================================== ============================================== ==============================================
Flowchart:

Java Code Editor:
Contribute your code and comments through Disqus.
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.
Java: Tips of the Day
Java: How to round a number to n decimal places in Java
Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output.
DecimalFormat df = new DecimalFormat("#.####"); df.setRoundingMode(RoundingMode.CEILING); for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) { Double d = n.doubleValue(); System.out.println(df.format(d)); }
Output:
12 123.1235 0.23 0.1 2341234.2125
For example, when you know that your values are accurate up to 6 digits, then to round half-way values up, add that accuracy to the value:
Double d = n.doubleValue() + 1e-6;
To round down, subtract the accuracy.
Ref: https://bit.ly/3a4mvz0
- Weekly Trends
- 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
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises