- To print X pattern using *
User Input: 3
Output:
* * * * *
#include <iostream>
using namespace std;
int main() {
int rows;
cout <<"Enter number of rows: ";
cin >> rows;
for(int i=0; i<rows; i++) {
for(int j=0; j<rows; j++) {
if(i==j||j==(rows-i-1))cout<<"* ";
else cout<<" ";
}
cout<<endl;
}
return 0;
}
Enter number of rows: 4 * * * * * * * *Program
Main Logic :
for(int i=0; i<rows; i++) {
for(int j=0; j<rows; j++) {
if(i==j||j==(rows-i-1))cout<<"* ";
else cout<<" ";
}
cout<<endl;
}
You can print any thing in this pattern, just replace your value with *