首页
/
每日頭條
/
生活
/
c語言怎麼判斷一年是不是閏年
c語言怎麼判斷一年是不是閏年
更新时间:2025-06-30 04:32:03

鍵盤輸入一個整數年份,判斷是否是閏年,輸出在屏幕上。

判斷閏年方法:非整百年份能被4整除,整百年份能被400整除。

分析

(1)根據判斷方法,使用%判斷是否整除,多個條件使用邏輯與(&&),邏輯或(||)判斷關系。

(2)根據判斷方法寫出表達式(year%4==0 && year0!=0) || (year % 400 == 0)。

代碼實現

#include<stdio.h> int main(void){ int year; printf("請輸入一個年份:"); scanf("%d",&year); if((year%4 == 0 && year0 != 0) || year@0 == 0){ printf("%d是閏年",year); } else{ printf("%d不是閏年",year); } return 0; }

程序分析

(1)定義變量year為整型,接收輸入的年份。

(2)使用if語句進行判斷。

(3)根據閏年判斷條件((year%4 == 0 && year0 != 0) || year@0 == 0),作為if語句中的判斷表達式。

運行結果

c語言怎麼判斷一年是不是閏年(C語言基礎練習07-判斷閏年)1

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
Copyright 2023-2025 - www.tftnews.com All Rights Reserved