C Sharp Program to print Number Triangle

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# Program to print Number Triangle

Like alphabet triangle, we can write the C# program to print the number triangle. The number triangle can be printed in different ways.

Let's see the C# example to print number triangle.

 
  1. using System;  
  2.   public class PrintExample  
  3.    {  
  4.      public static void Main(string[] args)  
  5.       {  
  6.        int  i,j,k,l,n;           
  7.        Console.Write("Enter the Range=");    
  8.        nint.Parse(Console.ReadLine());     
  9.        for(i=1; i<=n; i++)      
  10.        {          
  11.         for(j=1; j<=n-i; j++)      
  12.         {      
  13.          Console.Write(" ");      
  14.         }      
  15.         for(k=1;k<=i;k++)      
  16.         {      
  17.          Console.Write(k);      
  18.         }      
  19.         for(l=i-1;l>=1;l--)      
  20.         {      
  21.         Console.Write(l);      
  22.         }      
  23.         Console.Write(" ");      
  24.        }       
  25.    }  
  26.   }  

Output:

Enter the Range=5
     1
    121
   12321
  1234321
 123454321  
Enter the Range=6
      1
     121
    12321
   1234321 
  123454321
 12345654321