绝对值的定义式为
可以写一个if:
if (n < 0) {
n = -n;
}
也可以使用库函数fabs(浮点数绝对值函数)
在头文件
#include <cmath>
中
printf("%.2lf", fabs(n));
这里我使用第二种方法
得到代码:
#include <stdio.h>
#include <cmath>
using namespace std;
double n;
main() {
scanf("%lf", &n);
printf("%.2lf", fabs(n));
}