Mirrored Incrementing + Inverted Squared Number-Star

Understanding the Problem

The goal is to print a mirrored version of the incrementing and inverted squared number-star pattern.

Pattern Explanation

The pattern follows a grid structure where numbers increase sequentially but are printed in reverse order for each row.

4*3*2*1
12*11*10*9
8*7*6*5
16*15*14*13
            

Observations:

  • The pattern consists of 4 rows and 4 columns.
  • Numbers increment from left to right across the grid.
  • Each row is printed in reverse order.
  • Each number is followed by a '*', except the last number in each row.

Algorithm

The pattern follows these steps:

  1. Initialize a number counter starting from 1.
  2. Store values in a 2D array as they increment.
  3. Loop through rows and print numbers in reverse order.
  4. Within each row, print numbers right to left with '*'.
  5. Move to the next line after printing each row.

Method 1: Using Nested Loops

This method first stores values in an array and then prints them in reverse order.

#include <stdio.h>

void printPattern(int n) {
    int arr[n][n], num = 1;
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = num++;
        }
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = n - 1; j >= 0; j--) {
            printf("%d", arr[i][j]);
            if (j > 0) printf("*");
        }
        printf("\n");
    }
}

int main() {
    int size = 4;
    printPattern(size);
    return 0;
}
            

Output:

4*3*2*1
12*11*10*9
8*7*6*5
16*15*14*13