Pascal Triangle Program in C#

One of the most thrilling variety styles is Pascal’s Triangle (named after Blaise Pascal, a well-known French Mathematician and truth seeker). Pascal’s Triangle became at first developed by means of the historic Chinese language, but Blaise Pascal changed into the primary individual to discover the significance of all the patterns it contained.

Source Code 


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

namespace PascalTriangle
{
    class Program
    {
        static void Main(string[] args)
        {
            int row;
            int position;
                Console.Write("Please input a row Number : ");
                row = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please input  position along the row # "+row+": ");
                position = Convert.ToInt32(Console.ReadLine());
                if (row < position)
                {
                    Console.Write( "\nRow : " + row);
                    Console.Write("\nPosition: " + position);

                    Console.Write( "\nInvalid entry.  Position must be less than ( < ) or equal to ( = ) Row.");
                    int a1 = Convert.ToInt32(Console.ReadLine());
                    return;
                }
                else
                {
                    Console.Write("\nValue at row " + row + " and position " + position + " is " + compute_pascal(row, position));
                }
                int a = Convert.ToInt32(Console.ReadLine());
        }

        private static int compute_pascal(int row, int position)
        {
            if (position == 1)
            {
                return 1;
            }
            else if(position == row)
            {
                return 1;
            }
            else
            {
                return compute_pascal(row-1, position) + compute_pascal(row-1, position-1);
            }
        }
    }
}


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment