Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,236 questions

56,139 answers

573 users

How to create an alphabet rangoli (geometric and character pattern) of size N in C#

1 Answer

0 votes
using System;
using System.Text;

class AlphabetRangoli
{
    // Function to generate and print the alphabet rangoli of size N
    static void printAlphabetRangoli(int n)
    {
        if (n <= 0) return;

        // Total width of the grid based on character and hyphen spacing
        int total_width = 4 * n - 3;

        // Loop from -(n-1) to (n-1) to handle top/bottom symmetry mathematically
        for (int i = -(n - 1); i <= (n - 1); ++i)
        {
            int current_row_dist = Math.Abs(i); // Distance from the center row

            StringBuilder line_chars = new StringBuilder();

            // 1. Build the left/descending side of characters (e.g., e -> d -> c)
            for (int j = 0; j < n - current_row_dist; ++j)
            {
                line_chars.Append((char)('a' + n - 1 - j));
            }

            // 2. Build the right/ascending side of characters (e.g., d -> e)
            for (int j = n - current_row_dist - 2; j >= 0; --j)
            {
                line_chars.Append((char)('a' + n - 1 - j));
            }

            // 3. Insert hyphens between characters
            StringBuilder standard_row = new StringBuilder();
            for (int k = 0; k < line_chars.Length; ++k)
            {
                standard_row.Append(line_chars[k]);
                if (k != line_chars.Length - 1)
                {
                    standard_row.Append("-");
                }
            }

            // 4. Calculate necessary hyphen padding for centering
            int total_padding = total_width - standard_row.Length;
            string side_hyphens = new string('-', total_padding / 2);

            // 5. Print the complete constructed row
            Console.WriteLine(side_hyphens + standard_row + side_hyphens);
        }
    }

    static void Main()
    {
        int n = 5;

        printAlphabetRangoli(n);
    }
}


/*
run:

--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------

*/

 



answered Jul 12 by avibootz
edited Jul 12 by avibootz
...