Write a C++ program to print W star pattern using for loop. In this C++ example, printingSpaces iterate from 0 to rows and prints the empty spaces and printingStars will print the stars.
#include<iostream> using namespace std; void printingStars(int rows) { for (int i = 0; i < rows; ++i) { cout << "*"; } } void printingSpaces(int rows) { for (int i = 0; i < rows; ++i) { cout << " "; } } int main() { int rows; cout << "Enter W Shape Star Pattern Rows = "; cin >> rows; cout << "Printing W Shape Pattern\n"; for (int i = 0; i < rows; i++) { printingStars(i + 1); printingSpaces(rows - i - 1); printingStars(rows - i + 1); printingSpaces(2 * i); printingStars(rows - i); printingSpaces(rows - i - 1); printingStars(i + 1); cout << "\n"; } }
This C++ program prints the alphabetical W pattern of stars using a while loop.
#include<iostream> using namespace std; void printingStars(int rows) { int i = 0; while (i < rows) { cout << "*"; ++i; } } void printingSpaces(int rows) { int i = 0; while (i < rows) { cout << " "; ++i; } } int main() { int rows; cout << "Enter W Shape Star Pattern Rows = "; cin >> rows; cout << "Printing W Shape Pattern\n"; int i = 0; while (i < rows) { printingStars(i + 1); printingSpaces(rows - i - 1); printingStars(rows - i + 1); printingSpaces(2 * i); printingStars(rows - i); printingSpaces(rows - i - 1); printingStars(i + 1); cout << "\n"; i++; } }
Enter W Shape Star Pattern Rows = 13
Printing W Shape Pattern
* *************************** *
** ************* ************ **
*** ************ *********** ***
**** *********** ********** ****
***** ********** ********* *****
****** ********* ******** ******
******* ******** ******* *******
******** ******* ****** ********
********* ****** ***** *********
********** ***** **** **********
*********** **** *** ***********
************ *** ** ************
*************** **************
In this C++ example, the printingStars function allows entering any character and prints the W pattern of a given character.
#include<iostream> using namespace std; void printingStars(int rows, char ch) { for (int i = 0; i < rows; ++i) { cout << ch; } } void printingSpaces(int rows) { for (int i = 0; i < rows; ++i) { cout << " "; } } int main() { int rows; char ch; cout << "Enter Character for W Pattern = "; cin >> ch; cout << "Enter W Shape Star Pattern Rows = "; cin >> rows; cout << "Printing W Shape Pattern\n"; for (int i = 0; i < rows; i++) { printingStars(i + 1, ch); printingSpaces(rows - i - 1); printingStars(rows - i + 1, ch); printingSpaces(2 * i); printingStars(rows - i, ch); printingSpaces(rows - i - 1); printingStars(i + 1, ch); cout << "\n"; } }
Enter Character for W Pattern = $
Enter W Shape Star Pattern Rows = 16
Printing W Shape Pattern
$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $
$$ $$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$ $$
$$$ $$$$$$$$$$$$$$$ $$$$$$$$$$$$$$ $$$
$$$$ $$$$$$$$$$$$$$ $$$$$$$$$$$$$ $$$$
$$$$$ $$$$$$$$$$$$$ $$$$$$$$$$$$ $$$$$
$$$$$$ $$$$$$$$$$$$ $$$$$$$$$$$ $$$$$$
$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$ $$$$$$$
$$$$$$$$ $$$$$$$$$$ $$$$$$$$$ $$$$$$$$
$$$$$$$$$ $$$$$$$$$ $$$$$$$$ $$$$$$$$$
$$$$$$$$$$ $$$$$$$$ $$$$$$$ $$$$$$$$$$
$$$$$$$$$$$ $$$$$$$ $$$$$$ $$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$ $$$$$ $$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$ $$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$$$ $$$$ $$$ $$$$$$$$$$$$$$
$$$$$$$$$$$$$$$ $$$ $$ $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$