Please I want to make Pascal's triangle in console application

Recommended Answers

All 5 Replies

That is a very noble and honorable plan you have there. What is the code you have so far? What are the difficulties you encounter?
Please let us know.

Best to work it all out on paper first :)

I had the code but it wasn't in the center

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x;
            Console.WriteLine("Enter the number of rows ");
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    x = combination(i, j);
                    if (j == i)
                        Console.WriteLine(x);
                    else
                        Console.Write(x);
                }
            }
        }
        static int factorial(int n)
        {
            if (n == 0)
                return 1;
            else
            {
                int fact = 1;
                for (int i = 1; i <= n; i++)
                    fact *= i;
                return fact;
            }
        }
        static int combination(int n, int r)
        {
            return factorial(n) / (factorial(r) * factorial(n - r));
        }

    }
}

So what is your problem ?(besides the fact that your code is not put between code tags! Select your code and click the button marked #)
Your code works fine as is. The only thing that's missing as last line in your main function is Console.ReadKey(); or Console.ReadLine();
You will enjoy the outcome of your work more if you do that.

If its not centred, thats why I suggested you worked it out on paper first.. What you need to do before you do any code is work out how the logic helps you decide where you would start it if you were human.. Once you have it worked out, you can then explain it to a computer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.