2020年6月18日 星期四

C語言練習題11~閏年判斷1090618

1、判斷是否為閏年?
     程式範例~G:\我的雲端硬碟\c語言練習題\閏年判斷一次條件
//400的倍數 或 (4位倍數且不是100的倍數)才是閏年
#include <stdio.h>
int main(){
int year=0;
printf("輸人西元幾年=");
scanf("%d",&year);
if (year%400==0 || (year%4==0 && year%100!=0) ){
printf("%d年是閏年",year);
}
else{
printf("%d年不是閏年",year);
}
}
測試結果~
輸人西元幾年=1900
1900年不是閏年
------------------------
輸人西元幾年=2000
2000年是閏年
-------------------------------

2、輸入年月日計算是當年的第幾天?
      程式範例~G:\我的雲端硬碟\c語言練習題\輸入年月日計算是當年的第幾天
//輸入年月日計算是當年的第幾天
#include <stdio.h>
int main(){
int year=0,mon=0,day=0,leap=0,i=0;
int ans=0;
printf("input year mon day, space分開=");
scanf("%d%d%d",&year,&mon,&day);
if (year%400==0 || (year%4==0 && year%100!=0) ){
leap=1;
}
for (i=1;i<mon;i++){
if (i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12){
ans=ans+31;
}
if (i==4 || i==6 || i==9 || i==11){
ans=ans+30;
}
if (i==2 && leap==1){
ans=ans+29;
}
if (i==2 && leap==0){
ans=ans+28;
}
}
ans=ans+day;
printf("%d年%d月%d日\n是%d年的第%d天\n",year,mon,day,year,ans);
}
測試結果~
input year mon day, space分開=2020 6 14
2020年6月14日
是2020年的第166天
-----------------------------------
input year mon day, space分開=2019 1 5
2019年1月5日
是2019年的第5天

沒有留言:

張貼留言

Chrome教學5-建立桌面捷徑1110217

 建立桌面捷徑