Basic Programming C# Examples

C# Program to find If Given year is Leap year or not

The Program in C# Program to find If Given year is Leap year or not is given below:

using System;

namespace LeapYearCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family,enter a year to check if it is a leap year or not!: ");
            int year = Convert.ToInt32(Console.ReadLine());

            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
            {
                Console.WriteLine(year + " is a leap year.");
            }
            else
            {
                Console.WriteLine(year + " is not a leap year.");
            }

            Console.ReadLine();
        }
    }
}

Output:


Hello Codeauri Family,enter a year to check if it is a leap year or not!:
2020 is a leap year.

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line “using System;” specifies that the System namespace is being used in this code, which provides access to various classes and methods including Console for input/output.
  2. The code defines a namespace named “LeapYearCheck”.
  3. Inside the namespace, there is a class named “Program” that contains the main entry point of the code.
  4. The Main method of the “Program” class is where the program’s logic starts.
  5. The program prompts the user to enter a year using the Console.WriteLine method.
  6. The year entered by the user is stored in the “year” variable after being converted from a string to an integer using the Convert.ToInt32 method.
  7. The code then checks if the year is a leap year using an if-else statement. A leap year is a year that is divisible by 4 but not by 100, or a year that is divisible by 400.
  8. If the year is a leap year, the program outputs a message to the console indicating that it is a leap year. If not, it outputs a message indicating that it is not a leap year.
  9. The Console.ReadLine method is called at the end of the Main method to pause the console window and wait for the user to press the Enter key before closing.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X