The word in X pattern writes a word along both diagonals of a square, so the letters cross in the middle. With PROGRAM, the X reads P to M from top left to bottom right and M to P from top right to bottom left.
Quick answer: For a word of length n, loop over an n × n grid. Print the letter at position j of the word when the cell is on a diagonal, j == i or j == n − i + 1, and a space everywhere else. An odd-length word gives a single letter in the middle.
This program is part of the String Patterns collection.
Table of contents8 sections
Word in X Pattern output
Output (word “PROGRAM”):
P M R A O R G O R R A P M

How the word in x pattern works
The two diagonals are the same ones used in the X (cross) pattern. The only change is what gets printed: instead of a star, each diagonal cell shows the character in its column. Because the letter comes from the column, both diagonals spell the word left to right on every row, which is why row 2 shows R on the left and A on the right.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
npositions in the row. Print characters of the word wherej = i or j = n − i + 1; print spaces everywhere else. - Move to the next line.
Key takeaways
- The X is the two diagonals:
j == iandj == n − i + 1. - Each diagonal cell prints the word’s letter for its column.
- An odd-length word, like PROGRAM, has one centre letter: G.
- In C the word is a
chararray andstrlengives its length.
Word in X Pattern in C
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "PROGRAM";
int n = strlen(s);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1)
printf("%c", s[j - 1]);
else
printf(" ");
}
printf("\n");
}
return 0;
}Word in X Pattern in C++
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "PROGRAM";
int n = s.length();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1)
cout << s[j - 1];
else
cout << " ";
}
cout << endl;
}
return 0;
}Word in X Pattern in Java
public class WordX {
public static void main(String[] args) {
String s = "PROGRAM";
int n = s.length();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1)
System.out.print(s.charAt(j - 1));
else
System.out.print(" ");
}
System.out.println();
}
}
}Word in X Pattern in Python
s = "PROGRAM"
n = len(s)
for i in range(1, n + 1):
for j in range(1, n + 1):
if j == i or j == n - i + 1:
print(s[j - 1], end="")
else:
print(" ", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
What happens with an even-length word?
The diagonals never share a cell, so the two middle rows each show two neighbouring letters, for example with CODE: the middle rows read ” OD” and ” OD”. The X still forms, without a single centre.
How do I use my own word?
Change the string at the top. The length is worked out from it, so the grid resizes automatically. Read it from the keyboard with scanf("%s", s) in C or input() in Python.
Why does each row show different letters on the left and right?
Because the letter is chosen by the column. The left diagonal is in column i and the right one in column n − i + 1, so they hold different letters of the word.






