Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp exercises: Check whether a given year is a leap year or not


C# Sharp Conditional Statement: Exercise-4 with Solution

Write a C# Sharp program to find whether a given year is a leap year or not.

Pictorial Presentation:

Conditional Statement: Check a given year is leap year or not.

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise4  
{  
    public static void Main() 
{
    int chk_year;
    Console.Write("\n\n");
    Console.Write("Check whether a given year is leap year or not:\n");
    Console.Write("----------------------------------------------");
    Console.Write("\n\n");
    Console.Write("Input an year : ");
    chk_year= Convert.ToInt32(Console.ReadLine());
 
    if ((chk_year % 400) == 0)
 Console.WriteLine("{0} is a leap year.\n", chk_year);
    else if ((chk_year % 100) == 0)
 Console.WriteLine("{0} is not a leap year.\n", chk_year);
    else if ((chk_year % 4) == 0)
 Console.WriteLine("{0} is a leap year.\n", chk_year);
    else
 Console.WriteLine("{0} is not a leap year.\n", chk_year);
}
}

Sample Output:

Check whether a given year is leap year or not:                                                               
----------------------------------------------                                                                                                         
Input an year : 2017                                                                                          
2017 is not a leap year.

Flowchart :

Flowchart: Check a given year is leap year or not.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program to check whether a given number is positive or negative.
Next: Write a C# Sharp program to read age of a candidate and determine whether it is eligible for casting his/her own vote.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.