Alphabet Diamond

Alphabet Diamond

Medium C Pattern Printing 50 views
Explanation Complexity

Problem Statement

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A     
Characters increment then decrement. Combine upper and lower pyramid.

Input Format

An integer n representing the height of the pattern (middle width based on n).

Output Format

Character pattern where letters increment then decrement, forming an upper and lower pyramid.

Constraints

• n ≥ 1

• Pattern is symmetric vertically and horizontally

Concept Explanation

The pattern is created by combining an upper pyramid and a lower inverted pyramid, where characters increase from A to a peak and then decrease back to A.

Step-by-Step Explanation

Upper Pyramid (including middle row)

1.Use an outer loop for rows i from 0 to n-1.

2.Print (n - 1 - i) leading spaces.

3.Print increasing characters from A to (A + i).

4.Print decreasing characters from (A + i - 1) back to A.

5.Move to the next line.

Lower Pyramid

1.Use an outer loop for rows i from n-2 down to 0.

2.Print (n - 1 - i) leading spaces.

3.Print increasing characters from A to (A + i).

4.Print decreasing characters from (A + i - 1) back to A.

5.Move to the next line.

Concept Explanation

The pattern is created by combining an upper pyramid and a lower inverted pyramid, where characters increase from A to a peak and then decrease back to A.

Step-by-Step Explanation

Upper Pyramid (including middle row)

1.Use an outer loop for rows i from 0 to n-1.

2.Print (n - 1 - i) leading spaces.

3.Print increasing characters from A to (A + i).

4.Print decreasing characters from (A + i - 1) back to A.

5.Move to the next line.

Lower Pyramid

1.Use an outer loop for rows i from n-2 down to 0.

2.Print (n - 1 - i) leading spaces.

3.Print increasing characters from A to (A + i).

4.Print decreasing characters from (A + i - 1) back to A.

5.Move to the next line.

Input / Output Format

Input Format
An integer n representing the height of the pattern (middle width based on n).
Output Format
Character pattern where letters increment then decrement, forming an upper and lower pyramid.
Constraints
• n ≥ 1

• Pattern is symmetric vertically and horizontally

Examples

Input:
Output:
A ABA ABCBA ABCDCBA ABCDEDCBA ABCDCBA ABCBA ABA A

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int n = 5; // Number of rows in the upper half

    // Upper half
    for (int i = 1; i <= n; i++) {
        // Spaces
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        
        // Increasing letters
        for (char ch = 'A'; ch < 'A' + i; ch++)
            printf("%c", ch);
        
        // Decreasing letters
        for (char ch = 'A' + i - 2; ch >= 'A'; ch--)
            printf("%c", ch);
        
        printf("n");
    }

    // Lower half
    for (int i = n - 1; i >= 1; i--) {
        // Spaces
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        
        // Increasing letters
        for (char ch = 'A'; ch < 'A' + i; ch++)
            printf("%c", ch);
        
        // Decreasing letters
        for (char ch = 'A' + i - 2; ch >= 'A'; ch--)
            printf("%c", ch);
        
        printf("n");
    }

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int n = 5; // Number of rows in the upper half

    // Upper half
    for (int i = 1; i <= n; i++) {
        // Spaces
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        
        // Increasing letters
        for (char ch = 'A'; ch < 'A' + i; ch++)
            printf("%c", ch);
        
        // Decreasing letters
        for (char ch = 'A' + i - 2; ch >= 'A'; ch--)
            printf("%c", ch);
        
        printf("n");
    }

    // Lower half
    for (int i = n - 1; i >= 1; i--) {
        // Spaces
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        
        // Increasing letters
        for (char ch = 'A'; ch < 'A' + i; ch++)
            printf("%c", ch);
        
        // Decreasing letters
        for (char ch = 'A' + i - 2; ch >= 'A'; ch--)
            printf("%c", ch);
        
        printf("n");
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.