呼叫子函式的範圍內閏年判斷
阿新 • • 發佈:2021-01-27
技術標籤:c
將判斷閏年的功能定義為子函式,通過呼叫該函式判斷輸入範圍內哪些是閏年,輸出並統計閏年的總數
#include <stdio.h>
#include <math.h>
int runnian (int year)
{
int flag = 0;
if( ( year % 4 == 0 && year % 100 !=0 ) || year % 400 == 0)
{
printf("%5d", year);
flag = 1;
}
return flag;
}
int main ()
{
int min , max , i , n = 0;
printf("請輸入年份範圍:\n");
scanf("%d%d", &min , &max);
printf("%d到%d之間閏年有:\n", min , max);
for( i = min; i <= max; i++)
{
if(runnian(i) == 1)
{
if(++n % 5 == 0)
printf("\n");
}
}
printf("\n%d到%d年之有%d個閏年。\n", min , max , n);
}