I have seen many of people asking to write a triangle program, during interviews and lab sessions. When we refer the internet, there are a lot of blogs describing how to write the triangle program. But sometimes, it’s a little bit difficult for beginners to understand. Here, I’m using the simplest way of writing triangle program in C#.
- The first "For loop" is for counting how many lines to be printed.
- The second "For loop" is going to print the triangle.
Just copy this code and run the program. You will easily understand.
- using System;
- namespace ConsoleApplication4 {
- class Program {
- static void Main(string[] args) {
- Console.WriteLine("Enter the Input Number");
- int count = Convert.ToInt32(Console.ReadLine());
- for (int i = 0; i <= count; i++) {
- for (int j = 0; j <= i; j++) {
- Console.Write("*");
- }
- Console.Write("\n");
- }
- System.Console.Read();
- }
- }
- }