Introduction

Let’s develop a simple C# console application that will find the perfect square count in the list.

Getting Started

In ASP.NET 5 we can create console applications. To create a new console application, first, open Visual Studio 2015. Create using File -> New -> Project.
create-console-application-asp.net
Now we will select the ASP.NET 5 Console Application, then click on the OK button.
select-asp.net-console-application
We need to include the following references in our application:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
In the main method, I have created 1 method.
  1. CheckPerfactSquare – To find the perfect square in given input.
Please refer to the code snippet below:
  1. static void Main(string[] args) {
  2. try {
  3. List < int > ArrList = new List < int > ();
  4. List < bool > FinalCount = new List < bool > ();
  5. int ListCount = Convert.ToInt32(Console.ReadLine().Trim());
  6. for (int i = 0; i < ListCount; i++) {
  7. int ListItem = Convert.ToInt32(Console.ReadLine());
  8. ArrList.Add(ListItem);
  9. }
  10. for (int j = 0; j < ArrList.Count; j++) {
  11. FinalCount.Add(CheckPerfactSquare(ArrList[j]));
  12. }
  13. Console.WriteLine("Result:" + FinalCount.Where(x => x.Equals(true)).Count());
  14. } catch (Exception oEx) {
  15. Console.WriteLine("Error:" + oEx.Message);
  16. }
  17. Console.ReadKey();
  18. }

Find the perfect square

The method definition for the CheckPerfactSquare() is given below.

Here I am using 1 as the parameter for the method.
  1. public static bool CheckPerfactSquare(int number)
  2. {
  3. double result = Math.Sqrt(number);
  4. bool isSquare = result % 1 == 0;
  5. return isSquare;
  6. }
Click on F5, execute the project and follow the console window. The output will be:
find-perfect-square-output

Summary

In this article, we learned how to find the perfect square when you want to begin work in the ASP.NET 5 console applications.