3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語 平方根を返すコードを自作する

Posted at
  • 数値の平方根を返す。
  • 平方根が無理数の場合は0

コード

int ft_sqrt(int nb)
{
    int raiz;

    raiz = 0;
    while (raiz * raiz < nb){
        raiz++;
        if (raiz * raiz == nb)
            return raiz;
        if (raiz * raiz > nb)
            return 0;
    }
    return 0;
}

所感

nb <= 0
とか挟まなくても
whileの最初の条件のところで、判定するような書き方をする。

if文を多様するのではなく、whileの条件式で判定できないのか、考えてから書く。

while直後にraiz++で1からスタートさせる書き方も良いと思う。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?