C语言实验——求绝对值(选择结构)
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
从键盘上输入任意一个整数,然后输出它的绝对值!
Input
从键盘上输入任意一个整数。
Output
输出它的绝对值。
Example Input
-4
Example Output
4
1、利用if和else来进行判断
#include<stdio.h> int main() { int a; scanf("%d",&a); if(a>0) { printf("%d\n",a); } else { printf("%d\n",-a); } return 0; }
2、本题考察的是选择结构,抛开题目要求也可以用数学函数来做
#include<stdio.h> #include<math.h>///数学函数头文件 int main() { int a,b; scanf("%d",&a); b=abs(a);///绝对值函数 printf("%d\n",b); return 0; }