How to Get the Specific Value in the Pascal’s Triangle in C#

This is the best and logic building program about Pascal triangle. Pascal’s triangle has all the outer edges set as 1 each row is derived from its above row. In this Program I am trying to find the element of the triangle by getting the row number and row index from the user.

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);
}
}
}
}

Output of the Program

How to Get the Specific Value in the Pascal’s Triangle in C#

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