Finding Perfect Square Count in ASP.NET

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.   
  7.   for (int i = 0; i < ListCount; i++) {  
  8.    int ListItem = Convert.ToInt32(Console.ReadLine());  
  9.    ArrList.Add(ListItem);  
  10.   }  
  11.   
  12.   for (int j = 0; j < ArrList.Count; j++) {  
  13.    FinalCount.Add(CheckPerfactSquare(ArrList[j]));  
  14.   }  
  15.   
  16.   Console.WriteLine("Result:" + FinalCount.Where(x => x.Equals(true)).Count());  
  17.  } catch (Exception oEx) {  
  18.   Console.WriteLine("Error:" + oEx.Message);  
  19.  }  
  20.  Console.ReadKey();  
  21. } 

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.


Similar Articles