Wednesday, 26 December 2012

Pascal's Triangle.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pascal
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, i, row, n;
            Console.WriteLine("Enter number of rows");

            row = Convert.ToInt32(Console.ReadLine());
            a = 1;
            i = 0;
            Console.WriteLine("Pascal Triangle");
            while (i < row)
            {
                for (b = 40 - 3 * i; b > 0; b--)
                {
                    Console.Write(" ");
                }
               
                for (n = 0; n <= i; n++)
                {
                    if ((n == 0) || (i == 0))
                    {
                        a = 1;
                    }
                    else
                    {
                        a = (a * (i - n + 1)) / n;
                    }
                    Console.Write(a);
                    Console.Write("     ");// give 5 spaces
                }

                Console.WriteLine();
                i++;

            }
            Console.ReadLine();

        }
       
    }
}

No comments:

Post a Comment