Triangle In C# - A Simplest Way To Do

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#.

  1. The first "For loop" is for counting how many lines to be printed.
  2. The second "For loop" is going to print the triangle.

Just copy this code and run the program. You will easily understand.

  1. using System;  
  2. namespace ConsoleApplication4 {  
  3.     class Program {  
  4.         static void Main(string[] args) {  
  5.             Console.WriteLine("Enter the Input Number");  
  6.             int count = Convert.ToInt32(Console.ReadLine());  
  7.             for (int i = 0; i <= count; i++) {  
  8.                 for (int j = 0; j <= i; j++) {  
  9.                     Console.Write("*");  
  10.                 }  
  11.                 Console.Write("\n");  
  12.             }  
  13.             System.Console.Read();  
  14.         }  
  15.     }  
  16. }  
C#

 

Ebook Download
View all
Learn
View all