Jump to content

Console C# Triangle

nobiggieBIG

Task 9.4 – Triangle

Tricky one this: Write a program that programmatically draws a triangle on the screen as below:

*

**

***

****

*****

******

*******

********

*********

**********

 

C#

A8-7600 {} Gigabyte FM2+ Board {} CX430 {} Corsair Vengeance LP 8gb {} MSI GTX 760 2GB {}

 

Console.WriteLine("C# is aids");

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Totalschaden1997 said:

So... what's your question?

Im trying fiddling with this, but I cant figure it out

 

            int userRows;
            int userCols;

            userCols = 10;
            userRows = 10 - 1;

            for (int row = 0; row < userRows; row = row - 1)
            {

                for (int col = 1; col < userCols; col = col + 1)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            Console.WriteLine("Press a key to stop");
            Console.ReadKey();

 

A8-7600 {} Gigabyte FM2+ Board {} CX430 {} Corsair Vengeance LP 8gb {} MSI GTX 760 2GB {}

 

Console.WriteLine("C# is aids");

 

Link to comment
Share on other sites

Link to post
Share on other sites

What you wrote  would probably print something like ********* in 10 lines. 

What I'd do is have a variable before the start of after the nested loop that increments by 1 every time the outer loop is called.

 int userRows;
            int userCols;

            userCols = 1;
            userRows = 10 - 1;

            for (int row = 0; row < userRows; row = row - 1)
            {
                for (int col = 1; col < userCols; col = col + 1)
                {
                    Console.Write("*");
                }
				userCols++;
                Console.WriteLine();
            }

 

 

Spoiler

CPU:Intel Xeon X5660 @ 4.2 GHz RAM:6x2 GB 1600MHz DDR3 MB:Asus P6T Deluxe GPU:Asus GTX 660 TI OC Cooler:Akasa Nero 3


SSD:OCZ Vertex 3 120 GB HDD:2x640 GB WD Black Fans:2xCorsair AF 120 PSU:Seasonic 450 W 80+ Case:Thermaltake Xaser VI MX OS:Windows 10
Speakers:Altec Lansing MX5021 Keyboard:Razer Blackwidow 2013 Mouse:Logitech MX Master Monitor:Dell U2412M Headphones: Logitech G430

Big thanks to Damikiller37 for making me an awesome Intel 4004 out of trixels!

Link to comment
Share on other sites

Link to post
Share on other sites

Would this do it?

 

for (int i = 0; i <= length; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            Console.ReadKey();

 

Motherboard Asus X99-Deluxe CPU Intel 5930K @ 3,6Ghz CPU-coolingCorsair H80i RAM Crucial 16GB
GPU 2x MSI GTX 770 2GB + GTX 580 3GB Monitors BenQ XL2411T @ 144Hz+ 2x 1080p@60Hz
Accessories Corsair K60 (MX Red), Logitech G500, Logitech G27, Blue Yeti, Astro A50

Link to comment
Share on other sites

Link to post
Share on other sites

            int i, j;

            for (i = 0; i < 10; i++)
            {
                for (j = 0; j <= 1 * i; j++)
                    Console.Write("*");

                for (j = 10 - i; j > 0; j--)
                    Console.Write(" ");

                Console.WriteLine();

            }

            Console.WriteLine("Press a key to stop");
            Console.ReadKey();

Got it

A8-7600 {} Gigabyte FM2+ Board {} CX430 {} Corsair Vengeance LP 8gb {} MSI GTX 760 2GB {}

 

Console.WriteLine("C# is aids");

 

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to be really cool you could even do something like:
 

using System;
        
public class Program
{
    public static void Main()
    {
        string triangle = "";
        for(int i = 0; i < 10; i++){;
            for(int j = 0; j < i+1; j++){
                triangle += "*";
            }
            triangle += "\n";
        }
      
      	Console.WriteLine(triangle);
    }
}


I wonder, how many different ways I could come up with to draw a triangle of asterisks to the screen before the end of the day. 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

47 minutes ago, straight_stewie said:

If you want to be really cool you could even do something like:
 


using System;
        
public class Program
{
    public static void Main()
    {
        string triangle = "";
        for(int i = 0; i < 10; i++){;
            for(int j = 0; j < i+1; j++){
                triangle += "*";
            }
            triangle += "\n";
        }
      
      	Console.WriteLine(triangle);
    }
}


I wonder, how many different ways I could come up with to draw a triangle of asterisks to the screen before the end of the day. 

Nitpitcky, but would change triangle += "\n"; to triangle += System.Environment,NewLine; (or whatever it is) to ensure new line correctness.

Link to comment
Share on other sites

Link to post
Share on other sites

If simplicity is key, there's always the handy string constructor for repeating characters "X" amount of times.

 

I'd do it like this:

public class Program
{
    public static void Main(string[] args)
    {
        for(int i =0; i < 10;i++)
            Console.WriteLine(new string('*', i+1));
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, XtremeDev said:

If simplicity is key, there's always the handy string constructor for repeating characters "X" amount of times.

 

I'd do it like this:


public class Program
{
    public static void Main(string[] args)
    {
        for(int i =0; i < 10;i++)
            Console.WriteLine(new string('*', i+1));
    }
}

 

ill look at this when i can get c# working on visual studio community

A8-7600 {} Gigabyte FM2+ Board {} CX430 {} Corsair Vengeance LP 8gb {} MSI GTX 760 2GB {}

 

Console.WriteLine("C# is aids");

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×