Hello All,

I have a question and need some help with a c# console application project that asks the user to enter the number of rows to calculate and display Pascal’s Triangle. What I really need help with is how to properly format the output in the shape of a pyramid. I have completed all of coding work (i think) on the alogorithm side for the formula in c#.

I just need help with getting the output to display the results like this below on the screen:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1


My visual c# code is below. I apologize for any mistakes due to me being new. I appreciate all the help. Thank you very much.

using System;
using System.Collections.Generic;
using System.Text;

public class Triangle
{
    public static void Main()
    {

        int n;

        Console.Write("Enter an integer bound: ");
        
        n = Convert.ToInt32(Console.ReadLine());


        for (int y = 0; y < n; y++)
        
            {
            int c = 1;
            Console.WriteLine("\n\n");// Added only this.
            
              for (int x = 0; x <= y; x++)
            {
                Console.Write(c);
                Console.Write("");

                c = c * (y - x) / (x + 1);
            }
            Console.Write(" ");
        }
        Console.Write("  ");


    }

}

Recommended Answers

All 7 Replies

Here is what the output should look like. It should look like a pyramid.

Example output of Pascal’s Triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

OK, so what problem are you having making it?

OK, so what problem are you having making it?

Thank you for the response Lizr.
The problem that i'm having is with the format of the output. The code generates the correct data, but the display/output does not look like a pyramid. I've tried for many hours and just can't figure out how to make the output look like a pyramid.

So, get yourself some squared graph paper, write the details in it.. see how you'd work out how to do it for yourself..

I tried on paper and I'm still having a hard time with it. I'm not sure how to make it output in a pyramid shape?? I'm very lost....

Thats a key thing you really need to see for yourself, sure, I could do you some code, but the problem is someone cant be there to do that "oh" thought for you..

Draw it on paper.. see what you did, and write down in english what you did..

Its a simple thing (honest!) but you need to work out how you work it out, and its not something you can be taught to see.. You have to keep trying

Dont worry about the numbers to start with.. get some square paper (important to use it as it will represent character positions) and then do 2 lines, then a 3 lines.. and work out why you're doing what you're doing.. work out the pattern.. and then its that you need to turn into code.

Thank you again Lizr for the help and hints. I really do appreciate it! I will continue my learning with new c# books and material!

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.