Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,791 answers

573 users

How to calculate square root (or floor square if not perfect square) of an integer in C#

1 Answer

0 votes
using System;

class Program
{
    static int sqrt_(int n) { 
        if (n == 0 || n == 1) 
            return n; 
       
        int i = 1, sq = 1; 
         
        while (sq <= n) { 
          i++; 
          sq = i * i; 
        } 
        return i - 1; 
    } 
    
    static void Main()
    {
        Console.WriteLine(sqrt_(9));
        Console.WriteLine(sqrt_(5));
        Console.WriteLine(sqrt_(26));
        Console.WriteLine(sqrt_(16));
    }
}



/*
run:

3
2
5
4

*/

 





answered May 6, 2019 by avibootz
...