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:
- using System;
- using System.Collections.Generic;
- 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:
- static void Main(string[] args)
- {
- try
- {
- PrintPyramid();
- ConvertPyramidToSquare();
- }
- catch (Exception Ex)
- {
- Console.WriteLine("Error:" + Ex.Message);
- }
- Console.ReadKey();
- }
Print the Triangle
The method definition for the PrintPyramid() is given below:
- public static void PrintPyramid()
- {
- int i, j, k, l, n;
- Console.Write("Enter the Range = ");
- n = int.Parse(Console.ReadLine());
- for (i = 1; i <= n; i++)
- {
- for (j = 1; j <= n - i; j++)
- {
- Console.Write(" ");
- }
- for (k = 1; k <= i; k++)
- {
- Console.Write(k);
- }
- for (l = i - 1; l >= 1; l--)
- {
- Console.Write(l);
- }
- Console.Write("\n");
- }
- }
Convert Pyramid to square count
The method definition for the ConvertPyramidToSquare() is given below
- public static void ConvertPyramidToSquare()
- {
- Console.Write("Enter the Level = ");
- int a = int.Parse(Console.ReadLine());
- int i, j, count = 0;
- for (i = 1; i <= a; i++)
- {
- for (j = 1; j <= (2 * i) - 1; j++)
- {
- }
- while (j <= a)
- {
- count++;
- j++;
- }
- }
- 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