新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c语言如何求绝对值,c语言求绝对值的简单程序

发布时间:2023-12-27 03:11:47

c语言如何求绝对值

在C语言中,可使用abs函数来求一个整数的绝对值。

#include <stdio.h>
#include <stdlib.h>

int main() {
    int num = ⑸;
    int absolute = abs(num);
    printf("The absolute value of %d is %d
", num, absolute);
    return 0;
}

输出结果为:

The absolute value of ⑸ is 5

对浮点数的绝对值,可使用fabs函数。

#include <stdio.h>
#include <math.h>

int main() {
    float num = ⑶.14;
    float absolute = fabs(num);
    printf("The absolute value of %.2f is %.2f
", num, absolute);
    return 0;
}

输出结果为:

The absolute value of ⑶.14 is 3.14