Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ Alphabet Pattern 27

Posted in C++ Tutorial
Updated on Jan 10, 2024
By Mari Selvan
👁️ 116 - Views
⏳ 4 mins
💬 1 Comment
C++ Alphabet Pattern 27

Photo Credit to CodeToFun

C++ Alphabet Pattern 27

Here`s a program that prints the above alphabet pattern using C++ Programming:

example.cpp
Copied
Copy To Clipboard
#include <iostream>><
#include <iomanip>
using namespace std;

int main() {
  int i, j, k;
  for (i = 65; i <= 69; i++) {
    for (j = 69; j > i; j--)
      cout << " ";
    for (k = 65; k <= i; k++)
      cout << setw(2) << char(k);
    cout << "\n";
  }
  return 0;
}

💻 Testing the Program

When you run the above program, it will print the following output:

Output
    A
   A B
  A B C
 A B C D
A B C D E

🧠 How the Program Works

Let's break down the logic behind the code:

  1. The program includes two header files: <iostream> and <iomanip>. These headers provide functionality for input/output operations and formatting respectively.
  2. The using namespace std; statement allows the program to use names from the standard namespace, such as cout and endl, without explicitly specifying the namespace.
  3. The main() function is the entry point of the program. It returns an integer value to indicate the status of the program execution. In this case, it always returns 0 to indicate successful execution.
  4. Inside the main() function, three integer variables i, j, and k are declared. These variables will be used in the subsequent loops.
  5. The outer loop is a for loop with an initialization expression i=65, a condition i<=69, and an increment expression i++. It iterates over the values from 65 to 69 (inclusive), which correspond to the ASCII values of characters 'A' to 'E'.
  6. Inside the outer loop, there is another loop (inner loop) that prints spaces before the characters. This loop is a for loop with an initialization expression j=69, a condition j>i, and a decrement expression j--. It starts with the value 69 (ASCII value of 'E') and decrements j until it is greater than i.
  7. Inside the inner loop, the statement cout << " "; is executed, which outputs a space character. This produces the required indentation before the characters in each line.
  8. After the inner loop, there is another loop (inner loop) that prints the characters. This loop is a for loop with an initialization expression k=65, a condition k<=i, and an increment expression k++. It starts with the value 65 (ASCII value of 'A') and increments k until it reaches the value of i.
  9. Inside the inner loop, the statement cout << setw(2) << char(k); is executed. Here, setw(2) is a function from the <iomanip> header that sets the width of the output to 2 characters. The char(k) converts the integer value k to the corresponding character based on the ASCII encoding. The resulting character is then outputted using cout.
  10. After the inner loop, the statement cout << "\n"; is executed, which outputs a newline character. This moves the output to the next line after printing the characters for the current line.
  11. The outer loop continues to iterate until the condition i<=69 is no longer true. As a result, the entire pattern of characters is printed.
  12. Finally, the return 0; statement is encountered, which terminates the main() function and returns the value 0 to the operating system, indicating successful program execution.

💯 Tips for Enhancement:

Explore the versatility of this pattern by adjusting its parameters. Whether you increase or decrease the size, tweak the spacing, or modify the characters used, each change opens up a world of possibilities, allowing you to customize and create your unique visual effects.

✔ Conclusion:

Creating visually appealing patterns is not only a fun endeavour but also a great way to enhance your programming or design skills. We hope this tutorial has inspired you to explore the world of creative coding. Share your creations with us, and let your imagination run wild!

🤗 Closing Call-to-Action:

We'd love to see your unique interpretations of the alphabet pattern. Share your creations in the comments below, and don't hesitate to reach out if you have any questions or suggestions for future tutorials. Happy coding!

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (C++ Alphabet Pattern 27) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy