VB.Net program to check leap year

Here, we are going to learn how to check leap year in VB.Net?
Submitted by Nidhi, on December 02, 2020 [Last updated : February 18, 2023]

Checking leap year

Here, we will read a year from the user and then find the given year is a leap year or not and then print the appropriate message on the console screen.

VB.Net code to check leap year

The source code to check the entered year is a leap year or not is given below. The given program is compiled and executed successfully.

'VB.Net program to check the given year is a leap year or not. 

Module Module1

    Sub Main()
        Dim year As Integer = 0
        
        Console.Write("Enter year: ")
        year = Integer.Parse(Console.ReadLine())

        If ((year Mod 4 = 0) AndAlso (year Mod 100 <> 0)) OrElse ((year Mod 4 = 0) AndAlso (year Mod 100 = 0) AndAlso (year Mod 400 = 0)) Then
            Console.WriteLine("Entered year is leap year")
        Else
            Console.WriteLine("Entered year is not leap year")
        End If
    End Sub
    
End Module

Output:

Enter year: 2004
Entered year is leap year
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains a method Main(). In the Main() method, we created an integer variable year with an initial value 0.

Console.Write("Enter year: ")
year = Integer.Parse(Console.ReadLine())

Here, we read the value of variable year from user.

If ((year Mod 4 = 0) AndAlso (year Mod 100 <> 0)) OrElse 
((year Mod 4 = 0) AndAlso (year Mod 100 = 0) AndAlso (year Mod 400 = 0)) Then
    Console.WriteLine("Entered year is leap year")
Else
    Console.WriteLine("Entered year is not leap year")
End If

Here, we use if statements to check given year is leap year or not, and then we printed appropriate message on the console screen.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.