Open In App

C Program to Print Cross or X Number Pattern

Last Updated : 19 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Write a C program to print X number pattern series using a loop. How to print the X number pattern series using for loop in C program. Logic to Print X using numbers in C programming

Input 

N = 5

Output:

 

The pattern consists of Exactly n*2-1 rows and columns. Hence outer loop run rows with(for i=1;i<=count;i++) and run inner loop as for(j=1;j<=count;j++)

Example:

C




// C program to print cross number pattern
 
#include <stdio.h>
 
int main()
{
    int i, j;
    int count;
 
    int N = 5;
 
    count = N * 2 - 1;
 
    for (i = 1; i <= count; i++) {
        for (j = 1; j <= count; j++) {
            if (j == i || (j == count - i + 1)) {
                printf("%d", i);
            }
            else {
                printf(" ");
            }
        }
 
        printf("\n");
    }
 
    return 0;
}


Output

1       1
 2     2 
  3   3  
   4 4   
    5    
   6 6   
  7   7  
 8     8 
9       9

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads