Q1: Printing Simple Pattern You are required to recreate these exact patterns using nested loops on the terminal. Important Notes: 1. Hard-coding these patterns will result in zero marks. 2. The use of setw() and setfill() functions is strictly prohibited and will result in zero marks. 1) A) [10 Marks] B) [10 Marks] C) [10 Marks] --------------------------------------------------------------------------------------------------------------------------------------- 2) [20 Marks] The pattern includes specific colors. To achieve this, use the command: cout << "\033[92m" << "Hello World" << "\033[0m"; Here: ‘\033[92m’ applies a shade of green to the spaces. ‘\033[0m’ resets the color to default after each colored segment. Note: You may refer to this guide on GeeksforGeeks for help on changing console colors
Here are the solutions to the given problems: ### Q1: Printing Simple Pattern #### A) [10 Marks] ```cpp #include <iostream> int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { std::cout << "* "; } std::cout << std::endl; } return 0; } ``` This will print a pattern of stars like this: ``` * * * * * * * * * * * * * * * ``` #### B) [10 Marks] ```cpp #include <iostream> int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5 - i; j++) { std::cout << " "; } for (int k = 1; k <= i; k++) { std::cout << "* "; } std::cout << std::endl; } return 0; } ``` This will print a pattern of stars like this: ``` * * * * * * * * * * * * * * * ``` #### C) [10 Marks] ```cpp #include <iostream> int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5 - i; j++) { std::cout << " "; } for (int k = 1; k <= i; k++) { std::cout << "C "; } std::cout << std::endl; } return 0; } ``` This will print a pattern of 'C's like this: ``` C C C C C C C C C C C C C C C ``` ### Q2: Printing Pattern with Colors [20 Marks] ```cpp #include <iostream> int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5 - i; j++) { std::cout << "\033[92m \033[0m"; } for (int k = 1; k <= i; k++) { std::cout << "\033[92mHello World\033[0m "; } std::cout << std::endl; } return 0; } ``` This will print a pattern of 'Hello World' with green color like this: ``` Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World ``` Note: The color codes used here are ANSI escape codes. They may not work on all systems or terminals.