skip to Main Content

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

Answers


  1. 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

    Login or Signup to reply.
  2. You should escape the backslashes, try this:

    Console.WriteLine("   /\    ");
    Console.WriteLine("  /  \   ");
    Console.WriteLine(" /    \  ");
    Console.WriteLine("/______\ ");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search