You must Sign In to post a response.
  • Category: [Competition Entries]

    Reverse triangle program to display *

    Hi ,

    I need a program which has to display triangle in reverse

    eg
    *****
    ***
    *


    Thanks Inadvance,
    sachin
  • #703585
    you can try the below code
    input is the number of lines you want.


    private void DrawReverse(int lines)
    {
    for (int i = 0; i < lines; i++)
    {
    int count = (lines-i) * 2 - 1;
    string str = new string('*', count);
    Console.WriteLine(str);
    }
    }

    Regards,
    Shine

  • #703608
    try like this code


    private void Button1_Click_1(object sender, EventArgs e)
    {
    label1.Text = "";
    for (int i = 5; i >= 1; i--)
    {
    if (i % 2 == 1)
    {
    for (int j = i; j >= 1; j--)
    {
    label1.Text += "*";
    }
    }
    else
    {
    label1.Text += "\n";
    }
    }
    }

    Regards
    N.Ravindran
    Your Hard work never fails

  • #703619
    Hi,


    you can refer the below..

    Console.WriteLine("Enter the number:");
    string count = Console.ReadLine();
    for(int i=count; i>0; (i-2))
    {
    for(int j=i; j>0; j--)
    {
    Console.Write("*");

    }
    Console.Write("\n");
    }



    Hope this will help you

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #703648
    Logic is simple just you have to iterate a loop in reverse order and your triangle is ready
    check following code snippet


    for(int iCount=5; iCount>0; (iCount-2))
    {
    for(int jCount=iCount; jCount>0; jCount--)
    {
    Console.Write("*");

    }
    Console.Write("\n");
    }


    Hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.