I want to draw a pyramid using this 2 shapes { /\ } in c# but i got an error

71 Views Asked by At

Hi i wanna draw a pyramid that look like this

                                          /\ 
                                         /  \
                                        /    \
                                       /______\

in Microsoft visual studio c# language but it's not working

i used this code in Microsoft visual studio

using System;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("   /\ ");
            Console.WriteLine("  /  \ ");
            Console.WriteLine(" /    \ ");
            Console.WriteLine("/______\ ");

            Console.ReadLine();
        }
    }
}

but it's not working

please explain to me if possible why not working why your code is working in other words (what i need to know )
2

There are 2 best solutions below

1
Narish On

You need to escape backslashes, or use the @ literal modifier. The backslash is used to do things like specifying a newline "\n", so the compiler needs to be told explicitly when it write it out verbatim

This uses a multiline string literal

string p = @"
   /\ 
  /  \ 
 /    \ 
/______\ 
";
Console.WriteLine(p);

This is a full list of characters that require the escape sequence to ensure they are written verbatim

0
Yavuz On

You should escape the backslashes, try this:

Console.WriteLine("   /\\    ");
Console.WriteLine("  /  \\   ");
Console.WriteLine(" /    \\  ");
Console.WriteLine("/______\\ ");