By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Problem solving in C# Task - Magic Wand

+3 votes

As we all know programmers often make mistakes in their code. They spend hours and hours trying to figure out where the problem is. Some are praying for the code to fix itself, others are searching for magical rainbow unicorns to help them with their problem. One day, the programmers Gesho and Posho discovered a way to build magic wands that solve their coding problems. Your task is to help Gesho and Posho to build a magic wand.


The input data should be read from the console.

On the only input line you have an integer number N. The width of the wand is 3*N+2.

The input data will always be valid and in the format described. There is no need to check it explicitly.


The output data should be printed on the console. You must print the magic wand on the console.

Each row contains characters "." (dot) and "*" (asterisk).


  • The number N will always be an odd integer number in the range [5…39].
  • Allowed working time for your program: 0.25 seconds.
  • Allowed memory: 16 MB.


magic wand in c sharp - output

asked in C# category by user hues
edited by user golearnweb

1 Answer

+1 vote
Best answer

To solve this type of tasks - first you need to split the task onto smaller pieces!

Some of the rows are static - so they need to be printed on one row - just once;

The other ones are dynamic - so they will need for loops - from there you can use the "i" iterator to "connect the dots" :-) Your brain must be working on your full potential so you can find the logic in the figure between the "." and "*"; 

I am giving you my solution - see the comments in green! I have devided the figure in 8 parts - some are static: the 1st,3rd ,6th and the 8th;

And the other are dynamic: 2nd, 4th, 5th and the 7th - so they will need for loop.

In the code itself - you can read more about the rows:

Here is a picture of how I imagine this Magic Wand - in yellow - are the static rows and in the other colors are the dynamic ones:

magicwand output c harp

And here is the code itself:

using System;

class MagicWand
    static void Main()
        int n = int.Parse(Console.ReadLine());

        Console.WriteLine("{0}*{0}",//THE 1ST PART - STATIC - ONE ROW
            new string('.', (n * 3 + 1) / 2));

        for (int i = 0; i < (n / 2 + 1); i++)//THE 2ND PART (DYNAMIC + FOR LOOP)
                new string('.', ((n * 3 + 1) / 2) - 1 - i),
                new string('.', 1 + 2 * i));

        Console.WriteLine("{0}{1}{0}",//THE 3RD PART - STATIC - ONE ROW
            new string('*', (n)),
            new string('.', (n + 2)));

        for (int i = 0; i < n / 2; i++)//THE 4TH PART (DYNAMIC + FOR LOOP)
                new string('.', 1 + i),
                new string('.', 3 * n - 2 - 2 * i));

        for (int i = 0; i < n / 2; i++)//THE 5TH PART (DYNAMIC + FOR LOOP)
                new string('.', n / 2 - 1 - i),
                new string('.', n / 2),
                new string('.', 0 + i),
                new string('.', n));
        Console.WriteLine("{0}{1}*{2}*{1}{0}",//THE 6TH PART - STATIC - ONE ROW
                new string('*', n / 2 + 1),
                new string('.', n / 2),
                new string('.', n));

        for (int i = 0; i < n; i++)//THE 7TH PART (DYNAMIC + FOR LOOP)
                new string('.', n));
        Console.WriteLine("{0}{1}{0}",//THE 8TH PART - STATIC - ONE ROW
            new string('.', n),
            new string('*', n + 2));
answered by user nikole
edited by user golearnweb