C++ Program to Print Cross or X Pattern Last Updated : 30 Oct, 2023 Comments Improve Suggest changes Like Article Like Report Given a number n, we need to print an X pattern of size n. Input : n = 3Output : $ $ $ $ $Input : n = 5Output : $ $ $ $ $ $ $ $ $Input : n = 4Output : $ $ $$ $$ $ $ We need to print n rows and n columns. So we run two nested loops. The outer loop prints all rows one by one (runs for i = 1 to n). The inner loop (runs for j = 1 to n) runs all columns of current row. Now a row can contain spaces and '$'. How do we decide where to put space and where '$'. For i = 1 : First and last column should contain '$' For i = 2 : Second and second last column should contain '$' In general, i-th and (n + 1 - i)-th columns should contain '$' C++14 // Program to make an X shape $ pattern in c++ #include <iostream> using namespace std; void printPattern(int& n) { // Print all rows one by one for (int i = 1; i <= n; i++) { // Print characters of current row for (int j = 1; j <= n; j++) { // For i = 1, we print a '$' only in // first and last columns // For i = 2, we print a '$' only in // second and second last columns // In general, we print a '$' only in // i-th and n+1-i th columns if (j == i || j == (n + 1 - i)) cout << "$"; else cout << " "; } // Print a newline before printing the // next row. cout << endl; } } // Driver Code int main() { // n denotes the number of lines in which // we want to make X pattern int n = 6; // Function Call printPattern(n); return 0; } Output$ $ $ $ $$ $$ $ $ $ $Time Complexity: O(n2), where n represents the given input.Auxiliary Space: O(1), no extra space is required, so it is a constant. C++ Program to Print Cross or X Pattern Comment More info V vanshgaur14866 Follow Improve Article Tags : C++ pattern-printing Explore C++ BasicsIntroduction to C++ Programming Language3 min readData Types in C++7 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++10 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples7 min read Like