How to find the count to convert a pyramid into a square using C#

Introduction 
 
Let’s develop a simple C# console application to find the count so that we can convert a pyramid into a square.
 
Getting Started
 
In ASP.NET 5 we can create console applications. To create a new console application, we first open Visual Studio 2015. Create it using File-> New-> Project.
 
 
 
Now we will select the ASP.NET 5 Console Application to create the console application, click on the OK button.
 
 
 
Include the following references in the application:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq; 
In the main method, I have created 2 methods
 
1.PrintPyramid – To print the triangle in given input.
2.ConvertPyramidToSquare = Convert triangle into square.
 
Please refer the code snippet below:
  1. static void Main(string[] args)  
  2. {  
  3. try  
  4. {  
  5. PrintPyramid();  
  6. ConvertPyramidToSquare();  
  7. }  
  8. catch (Exception Ex)  
  9. {  
  10. Console.WriteLine("Error:" + Ex.Message);  
  11. }  
  12. Console.ReadKey();  

Print the Triangle
 
The method definition for the PrintPyramid() is given below:
 
  1. public static void PrintPyramid()  
  2. {  
  3. int i, j, k, l, n;  
  4. Console.Write("Enter the Range = ");  
  5. n = int.Parse(Console.ReadLine());  
  6. for (i = 1; i <= n; i++)  
  7. {  
  8. for (j = 1; j <= n - i; j++)  
  9. {  
  10. Console.Write(" ");  
  11. }  
  12. for (k = 1; k <= i; k++)  
  13. {  
  14. Console.Write(k);  
  15. }  
  16. for (l = i - 1; l >= 1; l--)  
  17. {  
  18. Console.Write(l);  
  19. }  
  20. Console.Write("\n");  
  21. }  

Convert Pyramid to square count
 
The method definition for the ConvertPyramidToSquare() is given below
  1. public static void ConvertPyramidToSquare()  
  2. {  
  3.    Console.Write("Enter the Level = ");  
  4.    int a = int.Parse(Console.ReadLine());  
  5.    int i, j, count = 0;  
  6.    for (i = 1; i <= a; i++)  
  7.    {  
  8.       for (j = 1; j <= (2 * i) - 1; j++)  
  9.       {  
  10.       }  
  11.       while (j <= a)  
  12.       {  
  13.          count++;  
  14.             j++;  
  15.       }  
  16.    }  
  17.    Console.WriteLine("Result Count: " + count);  
Click on F5 and execute the project and follow the console window. The output will be:
 
 
 
I hope this article is helpful for beginners trying to find the count to convert a pyramid into a square when they want to begin working in ASP.NET 5 console applications. Thanks for reading.

Happy Coding