闰年问题不同

书读百遍,不收手过一遍,光看书只是理解了概念,但是没有去操作还是不行的,今天将if-else语句的使用技巧以及注意事项,算数运算符,逻辑运算符和条件运算符的使用、优先顺序进行了进一步的了解。
这是关于闰年问题的3种不同程序
【1】用逻辑表达式
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int year;))//能被4整除不能被100整除,能被400整除的就是闰年
scanf("%d", &year);
if ((year % 4 == 0 &&year % 100 != 0) || (year % 400 == 0))
printf("%d是闰年\n",year);
system(“pause”);
return 0;
}
【2】用if–else语句嵌套
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int year;
scanf("%d", &year);
if (year % 4 == 0)//能被4整除
{
if(year%100!=0)//能被4整除不能被100整除
printf(“yes”);//是闰年
else
{
if(year%400==0)//能被4整除,能被100整除,能被400整除
printf(“yes”);//是闰年
else //能被4整除,能被100整除,不能被400整除
printf(“no”);//不是闰年
}
}
else printf(“no”);//不能被4整除则不是闰年
system(“pause”);
return 0;
}

【3】和上个程序类似只是if–else语句里面的内容有所不同
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int year;
scanf("%d", &year);
if (year % 4 == 0 )
{
if (year % 100 == 0)
{
if (year % 400 == 0)
printf(“yes”);
else
printf(“no”);
}
else printf(“yes”);
}
else printf(“no”);
system(“pause”);
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43249530/article/details/82940346