if语句
- 编程: 输入成绩,要求根据成绩输出对应结果(如 “北京大学录取分数线” 、“福州大学录取分数线” 、“其他” ),其中700分以上为“北京大学录取分数线” ,500~700分之间为“福州大学录取分数线” ,500分以下为“其他”。
#include<stdio.h>
int main()
{
int score;
printf("score:");
scanf("%d",&score);
if(score>=500&&score<=700)
{
printf("fz");
}
else if(score>700)
{
printf("bd");
}
else if(score<500)
{
printf("other");
}
return 0;
}
- 编程:四则运算,输入一个形式如“操作数 运算符 操作数”的四则运算表达式(例如5+8),输出运算结果。
#include<stdio.h>
int main()
{
int a,b;
char x;
printf("输入一个四则运算表达式:");
scanf("%d%c%d",&a,&x,&b);
if(x=='+')
{
printf("%d\n",a+b);
}
else if(x=='-')
{
printf("%d\n",a-b);
}
else if(x=='*')
{
printf("%d\n",a*b);
}
else if(x=='/')
{
if(b==0)
{
printf("error\n");
}
else
{
printf("%d\n",a/b);
}
}
return 0;
}
- 编程:判断输入的年份是否为闰年。(闰年的条件是:能被4整除但不能被100整除;或者能被400 整除)。
#include<stdio.h>
int main()
{
int years;
printf("輸入年份:");
scanf("%d",&years);
if(years%400==0||years%4==0&&years%100!=0)
{
printf("ruan\n");
}
else
{
printf("ping\n");
}
return 0;
}
4.输入一个整数,判断它是奇数还是偶数,并输出判断结果。
#include<stdio.h>
int main()
{
int c;
printf("输入一个整数:");
scanf("%d",&c);
if(c%2==0)
{
printf("偶数\n");
}
else
{
printf("奇数\n");
}
return 0;
}