THANKS FOR WATCHING MY BLOG.IF ANY PROBLEM PLEASE COMMENT

Wednesday, 30 January 2013

Triangle representation – A sample program in C#

Description:



This triangle representation program is used to diplay the given character in a triangular format. In this program, no of rows is taken as input and For loop is used to represent the given character in a triangle format. Conslole.WriteLine is used to display  the given character and Console.ReadLine is used to get the input (no_of_rows) value. 



Sample code:



using System;

using System.Collections.Generic;

using System.Text;



namespace codefortriangleformat

{

    class Program

    {

        static void Main(string[] args)

        {

            string j;

            int k = 0;

            int m = 1;

            int i = 1;

            int y = 0;

            int a = 0;

            int no_of_rows = 0;

            Console.WriteLine(" Enter the number of rows ");

            j = Console.ReadLine();

            no_of_rows = Convert.ToInt32(j);

            a =  (2*no_of_rows) - 1;

            y = (a-1)/2;

            for (i = 1; i <= no_of_rows; i++)

            {

                    for (a = 0; a < y; a++)

                    {

                        Console.Write("  ");

                        Console.Write(" ");

                    }

                    for (k = 0; k < m; k++)



                {

                    Console.Write(" * ");

                   

                }

                    Console.WriteLine(" \n");

                    m = m + 2;

                    y--;

                }

            }

               

               

            }

           }







Sample Input :

Enter the number of rows: 5



Sample Output:



                                   

            *



         *  *  *



      *  *  *  *  *



   *  *  *  *  *  *  *



*  *  *  *  *  *  *  *  *




                            

No comments:

Post a Comment