Write a C++ program to print X star pattern using for loop.
#include<iostream> using namespace std; int main() { int i, j, val, rows; cout << "Enter X Pattern Rows = "; cin >> rows; cout << "X Pattern of Stars\n"; val = rows * 2 - 1; for(i = 1; i <= val; i++) { for(j = 1; j <= val; j++) { if(j == i || j == val - i + 1) { cout << "*"; } cout << " "; } cout << "\n"; } return 0; }
This C++ example prints the X pattern of a given character using a while loop.
#include<iostream> using namespace std; int main() { int i, j, val, rows; char ch; cout << "Enter X Pattern Rows = "; cin >> rows; cout << "Enter Symbol for X Pattern = "; cin >> ch; cout << "X Pattern of Stars\n"; val = rows * 2 - 1; for(i = 1; i <= val; i++) { for(j = 1; j <= val; j++) { if(j == i || j == val - i + 1) { cout << ch; } cout << " "; } cout << "\n"; } return 0; }
Enter X Pattern Rows = 10
Enter Symbol for X Pattern = #
X Pattern of Stars
# #
# #
# #
# #
# #
# #
# #
# #
# #
#
# #
# #
# #
# #
# #
# #
# #
# #
# #