w3resource

C++ Exercises: Print an American flag on the screen


Print American Flag Pattern

Write a C++ program to print an American flag on the screen.

Visual Presentation:

C++ Exercises: Print an American flag on the screen

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    cout << "\n\n Print the American flag:\n"; // Outputting a message indicating the purpose of the program
    cout << "-----------------------------\n"; // Displaying a separator line

    // Outputting the pattern to represent an American flag with asterisks and equal signs
    cout << "* * * * * * ==================================\n"; // First row of the flag
    cout << " * * * * *  ==================================\n"; // Second row of the flag
    cout << "* * * * * * ==================================\n"; // Third row of the flag
    cout << " * * * * *  ==================================\n"; // Fourth row of the flag
    cout << "* * * * * * ==================================\n"; // Fifth row of the flag
    cout << " * * * * *  ==================================\n"; // Sixth row of the flag
    cout << "* * * * * * ==================================\n"; // Seventh row of the flag
    cout << " * * * * *  ==================================\n"; // Eighth row of the flag
    cout << "* * * * * * ==================================\n"; // Ninth row of the flag

    // Outputting the lower part of the flag representing the stripes
    cout << "==============================================\n"; // Rows representing the stripes of the flag (bottom)
    cout << "==============================================\n";
    cout << "==============================================\n";
    cout << "==============================================\n";
    cout << "==============================================\n";
    cout << "==============================================\n";
    cout << "==============================================\n";

    return 0; // Return statement indicating successful completion of the program
}

Sample Output:

 Print the American flag:                                              
-----------------------------                                          
* * * * * * ==================================                         
 * * * * *  ==================================                         
* * * * * * ==================================                         
 * * * * *  ==================================                         
* * * * * * ==================================                         
 * * * * *  ==================================                         
* * * * * * ==================================                         
 * * * * *  ==================================                         
* * * * * * ==================================                         
==============================================                         
==============================================                         
==============================================                         
==============================================                         
==============================================                         
==============================================

Flowchart:

Flowchart: Print an American flag on the screen

For more Practice: Solve these Related Problems:

  • Write a C++ program to print a stylized American flag pattern using loops and conditional statements.
  • Write a C++ program that prints an American flag with alternating rows of stars and stripes using nested loops.
  • Write a C++ program to generate an ASCII art version of the American flag with proper spacing and alignment.
  • Write a C++ program that prints an American flag pattern and allows the user to change the size of the flag.

Go to:


PREV : Area and Perimeter of Rectangle.
NEXT : Reverse First and Last Name.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.