0

i can't solve my triangle program in school , it is printing one sided curve by this program using System;

namespace starpyramid
{ 
    class program 
    { 
        static void Main() 
        { 
            Console.Write("Height: "); 
            int i = int.Parse(Console.ReadLine());
            if(i>0)
            {
            goto main;
            }
            else
            {
                Main();
            }   

            main:
            Console.Write("\n");
            for (int h = 1; h<= i;h++) // main loop for the lines
            {
                for (int s = h; s <= i; s++) //for spaces before the stars
                {
                    Console.Write(" ");
                }
                for(int j=1; j<(h*2); j=j+2)
                {
                    Console.Write("*");
                }
            Console.Write("\n");
            }
    }   
    } 
}

but i need to modify it by something that make this a proper triangle !

Gilad Green
  • 36,708
  • 7
  • 61
  • 95

1 Answers1

0

Here is one solution:

public static void DrawPyramid(int Rows)
{
    string Pyramid = string.Empty;
    int n = Rows;
    for (int i = 0; i <= n; i++)
    {
        for (int j = i; j < n; j++)
        {
            Pyramid += " ";
        }
        for (int k = 0; k < 2 * i - 1; k++)
        {
            Pyramid += "*";
        }
        Pyramid += Environment.NewLine;
    }
    Pyramid.ConsoleWrite();
}

Example:

DrawPyramid(5);

//     *  
//    ***   
//   ***** 
//  *******
// *********
fubo
  • 44,811
  • 17
  • 103
  • 137