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();  
  13. } 
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. }  
  22. } 
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);  
  18. } 
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

Open Closed Principle

  Open Closed Principle We will be discussing the Open-Closed Principle also known as OCP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software. This principle said that should be open for extension but closed for modification. We should write a code that does not require modification every time a customer changes its request. Example Let us define a class that contains salary calculations. Let us imagine that we have a task where we need to calculate the total cost of all the developer salaries in a single company. To get started, we are going to create the model class first, public class EmployeeReport { public int Id { get ; set ; } public string Name { get ; set ; } public string Level { get ; set ; } public int WorkingHours { get ; set ; } public double HourlyRate { get ; ...

Liskov Substitution Principle

Liskov Substitution Principle We will be discussing the Liskov Substitution Principle also known as LSP, as one of the SOLID principles of object-oriented programming and how to implement it when designing our software. This principle said that Subtypes must be substitutable for their base types. In other words, if we substitute a superclass object reference with an object of any of its subclasses, the program should not break. Example Here we design three classes for employees and one abstract class and Implement abstract class on these classes, PermamentEmployee.cs ContractEmployee.cs TemporaryEmployee.cs class LSP { public LSP ( ) { Run ( ) ; } public void Run ( ) { Console . ReadLine ( ) ; } public abstract class Employee { protected string FullName { get ; set ; } protected abstract string CalculateBonus ( ) ; } public class PermamentEmployee : Employee ...