Skip to main content

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

Introduction 

Let’s develop a simple C# console application to find the count of 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

Comments

Popular posts from this blog

How to perform lazy loading with generic method.

Introduction Let’s develop C# console application to perform lazy loading with generic method.    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 because we will create the console application and click on the OK button. We need to include the following references in our application. using  System;   using  System.Collections.Generic;   using  System.Linq;  In the main method, I have created 2 classes and 1 method EducationProfile<T> – To define class members. Candiate<T> = Initialize lazy loading in the class. CallLazyLoading = call the function Please refer to the code snippet below static   void  Main( string [] args)   {        try ...

How to perform Insert/Update/Delete operation in Generic Collections in C#

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 because we will create the console application and click on the OK button. we need to include the following references in our application. using System; using System.Collections.Generic; Insert, Update, Delete, Get perform in List collection. public static void DML_List() { // Creating a List of integers IList<int> list = new List<int>(); // Printing the insert opeartion Console.WriteLine("INSERT"); // Adding elements in list collection list.Add(100); list.Add(200); list.Add(300); list.Add(400); list.Add(400); // Printing the element from list collection foreach (object x in list) { Console.WriteLine("Result : " + x); } Console.WriteLine("\n"); // Printing th...