eg.编写程序,判断输入的年份是否为闰年
1.标准格式
#include<stdio.h>
int main()
{ int year,leap;
printf("输入年份:");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0))
leap=1;
else
leap=0;
if(leap)
printf("%d是闰年\n",year);
else
printf("%d不是闰年\n",year);
return 0;
}
2.嵌套格式
#include<stdio.h>
int main()
{ int year,leap;
printf("输入年份:");
scanf("%d",&year);
if((year%4==0)
{ if(year%100==0)
{ if(year%400==0)
leap=1;
else
leap=0;
}
else
leap=1;
}
else
leap=0;
if(leap)
printf("%d是闰年\n",year);
else
printf("%d不是闰年\n",year);
return 0;
}
3.多分支格式
#include<stdio.h>
int main()
{ int year,leap;
printf("输入年份:");
scanf("%d",&year);
if(year%4!=0)
leap=0;
else if(year%100!=0)
leap=1;
else if(year%400!=0)
leap=0;
else
leap=1;
if(leap)
printf("%d是闰年\n",year);
else
printf("%d不是闰年\n",year);
return 0;
}