Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C에서 주어진 연도가 윤년인지 확인하는 프로그램

<시간/>

평년은 365일인 반면, 윤년은 366일이며 주어진 연도가 윤년인지 아닌지를 프로그램을 통해 확인하는 작업입니다.

그 논리는 연도를 400으로 나눈 것인지 4로 나누었는지 확인하는 것일 수 있지만 숫자를 둘 중 하나로 나누지 않으면 정상적인 연도가 됩니다.

예시

Input-: year=2000
Output-: 2000 is a Leap Year

Input-: year=101
Output-: 101 is not a Leap year

알고리즘

Start
Step 1 -> declare function bool to check if year if a leap year or not
bool check(int year)
   IF year % 400 = 0 || year%4 = 0
      return true
   End
   Else
      return false
   End
Step 2 -> In main()
   Declare variable as int year = 2000
   Set check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year)
   Set year = 10
   Set check(year)? printf("%d is a Leap Year",year): printf("\n%d is not a Leap Year",year);
Stop

예시

#include <stdio.h>
#include <stdbool.h>
//bool to check if year if a leap year or not
bool check(int year){
   // If a year is multiple of 400 or multiple of 4 then it is a leap year
   if (year % 400 == 0 || year%4 == 0)
      return true;
   else
      return false;
}
int main(){
   int year = 2000;
   check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year);
   year = 101;
   check(year)? printf("%d is a Leap Year",year): printf("\n%d is not a Leap Year",year);
   return 0;
}

출력

2000 is a Leap Year
101 is not a Leap Year