C# Help

Associate
Joined
26 Apr 2009
Posts
204
As part of an exercise we have been asked to create a triangle based on a user input and I'm quiet confused on where I'm going wrong :( Would anyone be able to point me in the right direction as to where I'm going wrong?

Below is the brief;
Exercise 5.3
Ask the user for the triangle size they wish to display. Display a triangle created out
of asterisks based on the size entered.
For example, for a request of 6 the triangle should be:
*
**
***
****
*****
******

This is the code I've gotten so far;
namespace Exercise_5_3
{
class Program
{
static void Main(string[] args)
{
int column, row, size;
string inputSize;

Console.WriteLine("Please enter the size of the triangle you wish to create");
inputSize = Console.ReadLine();
size = int.Parse(inputSize);


for (row = size; row < size; row--)
{

for (column = size; column < row - 1; column++)

{
Console.WriteLine("*");
}


}
Console.WriteLine("Triangle completed!");
Console.ReadLine();
}

}
}

Any help would be greatly appreciated
 
Each row has that row numbers amount of asterisks,

1 = *
2 = * *
3 = * * *

So you want to iterate over each row number and add that many asterisks to the output before writing a new console line.

Code:
foreach row
   while the row number (a counter) is greater than 0
      add an asterisk to the output
      decrement the row counter
   
    write a new console line

Hint: you need a copy of the row number (the 'counter' part) for the while loop
 
Last edited:
Just based on a quick glance at your code, I think you should have a look at what Console.WriteLine actually does.
Hint: Console.Write and Console.WriteLine are two different methods.

EDIT. And also check out about for loops, specificially the conditional bit.
 
Last edited:
I'm very new to C# myself, litterally just started learning it - so this might not be the best way of doing it, but this is how I've achieved it:

Code:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a number");
            int hight = int.Parse(Console.ReadLine());
            int checker = ((hight - hight) + 1);

            while (hight >= checker)
            {
                for (int i = 1; i <= checker; i++)
                {
                    Console.Write("*");
                }

                Console.Write("\n");
                checker++;
            }
        }
    }
}

Edit: Not sure why I didn't just do "int checker = 1;" ? lol...
 
Last edited:
You don't really need the checker logic there. It's writing 1 every time anyway ((1-1) + 1) = 1), ((4-4) + 1) = 1).

Personally I'd have the while loop inside the for because it's more semantically correct to me ("for each row.... while we still need to write asterisks ..... write").

As someone's already posted working code, I'll post the code to go with my psuedo example above.

Code:
Console.Write("Please enter the size of the triangle you wish to create: ");
var size = int.Parse(Console.ReadLine());

for (var row = 1; row <= size; row++)
{
    var counter = row;

    while (counter > 0)
    {
        Console.Write("*");
        counter--;
    }

    Console.WriteLine("\n");
}

Console.ReadLine();

The Console.ReadLine(); at the end stops the console closing straight away.
 
Last edited:
Would using the string builder object be more efficient for bigger triangles than echo'ing every "*"?

That would probably be the way I would do it.
Maybe have a method called BuildString that uses a StringBuilder to construct a string of appropriate length then simply call this in a loop for the number of rows.

That's mainly to keep the code clean, for the purposes of this sort of thing the performance gain would likely be negligible unless you've got thousands and thousands of rows.
 
Here's a java version I wrote in my Programming Fundamentals class in 2004 :)

Code:
// Filename: Pyramid.java
// Created: 8th November 2004
// Author: **
// Description: This program outputs a pyramid of characters

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Pyramid
{
	public static void main(final String[] pArgs) throws IOException
	{
		final BufferedReader tKeyboard = 
			new BufferedReader(new InputStreamReader(System.in));

// Get tBase and tChar from user
		System.out.print("Enter the base width: "); System.out.flush();
		final int tBase = Integer.parseInt(tKeyboard.readLine());

		System.out.print("Enter the character: "); System.out.flush();
		final char tChar = (tKeyboard.readLine()).charAt(0);

		iDoPyramid(tBase,tChar);
	}

// Creates a pyramid
	private static void iDoPyramid(final int pBase, final char pChar)
	{


// Create each row individually
		for(int tNoOfStars = 1; tNoOfStars<=pBase; tNoOfStars++)
		{
			String tRowOfStars = "";

// Positions the stars properly
			for(int tSpaces = tNoOfStars; tSpaces<pBase; tSpaces++)
			tRowOfStars = " " + tRowOfStars;

// Add each star, and trailing space, individually
			for(int tRow = tNoOfStars; tRow>0; tRow--)
			{
				tRowOfStars = tRowOfStars + pChar + " ";
			}

// Output each row
			System.out.println(tRowOfStars);
		}

	}
}
 
Back
Top Bottom