C Sharp program for creating a Pascal triangle ~ MiTechDev.com

Mitechdev.com is a programming blog which focused on Web Programming and development

Thursday, 2 February 2017

C Sharp program for creating a Pascal triangle

03:30:00 Posted by raviteja swayampu No comments
The following program print a Pascal triangle(Based on user input it print the number of rows).The pascal triangle is look like below.

The source code for printing triangle is as follows..
using System;
using System.Collections.Generic;
using System.Text;
 
namespace PascalTriangle
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("************Pascal Triangle Program*****************");
            System.Console.Write("Enter the number of rows: ");
            string input = System.Console.ReadLine();
 
            int n = Convert.ToInt32(input);
 
 
            for (int y = 0; y < n; y++)
            {
                int c = 1;
                for (int q = 0; q < n - y; q++)
                {
                    System.Console.Write("   ");
                }
 
                for (int x = 0; x <= y; x++)
                {
                    System.Console.Write("   {0:D} ", c);
                    c = c * (y - x) / (x + 1);
                }
                System.Console.WriteLine();
                System.Console.WriteLine();
            }
            System.Console.ReadLine();
        }
    }
}
Output:

0 comments:

Post a Comment