C语言的sqrt函数的调用

1.sqrt的含义:在math头文件中,sqrt(a)的含义是a的平方根。

2.对于sqrt函数的直接使用:需要在程序前加上头文件#include<math.h>代码如下

#include<stdio.h>
#include<math.h>                         //***头文件math***//
int main()
{
    int a=49,b;
    b=sqrt(a);                           //***直接调用math.h头文件***//
    printf("Output number is %d",b);
    return 0;
}

程序运行结果为:

 3.在没有头文件#include<math.h>的情况下:需要调用sqrt函数,并且把sqrt函数的逻辑编辑出来

代码如下:

#include<stdio.h>
int sqrt(int a);
int main()
{
    int a=49,b;                      //***把a和b都定义为int整形***//
    b=sqrt(a);
    if(b<0)                          //***这个if主要判断a是否能被开根***//
        printf("Error: sqrt returns %d\n",b);
    else
        printf("The squre root of %d is %d\n",a,b);

}
int sqrt(int a)
{
    int temp =a/2;                   //***先找temp的一半整形,可以减少运算***//
    while(temp--)
   {  
        if(temp*temp==a)            //***此时当temp--到7时,if语句成立,返回temp到b 
                                    中***//
            return temp;
    }
    return -1;                      //***当temp--为时,返回-1到b中***//
}

程序运行如下:

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值