C#

[백준] 2753번 윤년 C# 풀이

오늘의 기록, 오록 2019. 9. 24. 00:12

연도를 입력했을때, if문을 이용해서 윤년이면 1, 아니면 0을 출력하는 문제이다.

문제에서의 윤년 조건은 

  • 윤년은 연도가 4의 배수이면서
  • 100의 배수가 아닐때
  • 또는 400의 배수일 때

윤년의 조건을 if문으로 작성해주면 된다.

 

using System;
public class MainClass {
  public static void Main() {
    string value = Console.ReadLine();
    int year = Convert.ToInt32(value);
    
    if (year>=1 && year<=4000)
    {
      if (CheckValue(year))
      {
        Console.WriteLine(1);
      }
      else
      {
        Console.WriteLine(0);
      }
    }
  }
  
  private static bool CheckValue(int year)
  {
    bool result = false;
    
    if ((year%4)==0)
      result = true;
    else
      result = false;
     
     if (result)
     {
       if ((year%100== 0)
       {
         result = false;
       }
       
       if ((year%400== 0)
       {
          result = true;
       }
     }
    
    return result;    
  }
  
}
cs