Solution:NIIT/GNIIT Sonugiri0032@gmail.com

Tuesday, January 05, 2016

Pascal Triangle Program in C#

Pascal Triangle Program in C#


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:

Related Posts:

0 comments:

GNIITSOLUTION GNIIT SOLUTION. Powered by Blogger.

Translate

Blog Archive

undefined