Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

11,144 questions

15,208 answers

573 users

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

1 Answer

0 votes
#include <stdio.h>

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; 
} 
  
int main() 
{ 
    printf("%i\n", sqrt_(9));
    printf("%i\n", sqrt_(5));
    printf("%i\n", sqrt_(26));
    printf("%i\n", sqrt_(16));

    return 0; 
} 



/*
run:

3
2
5
4

*/

 




answered May 6 by avibootz
edited May 6 by avibootz
...