Table of contents
What is leap year?
Leap year is an year occurring once evry four years. This year has 29th day in february and 366 days in total.
Leap year logic
1.If the year is completely divisible by 4 then End 1.
2. If the year is completely divisible by 100.
3. If the year is completely divisible by 400 then it is a leap year End 3.
Else this is a leap year. End 3.
Else this is a not a leap year. End 1.
Prerequisite programming concepts
C program to check leap year
leap year program in c is as below:
#include<stdio.h>
int main(int argc, char const *argv[])
{
int year;
//input the year.
printf("C program to find leap year\nEnter a year\n");
scanf("%d",&year);
if( year % 4 == 0 )
{
if( year % 100 == 0 )
{
if( year % 400 == 0 )
printf( "%d is leap year" , year );
else
printf( "%d is not a leap year" , year );
}
else
{
printf( "%d is not a leap year" , year );
}
}
else
{
printf( "%d is not a leap year" , year );
}
return 0;
}
C++ program to check leap year
#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int year;
//input the year.
cout<<"C++ program to find leap year\nEnter a year\n";
cin>>year;
if( year % 4 == 0 )
{
if( year % 100 == 0 )
{
if( year % 400 == 0 )
cout<<year <<" is leap year\n";
else
cout<<year <<" is not a leap year\n";
}
else
{
cout<<year <<" is leap year\n";
}
}
else
{
cout<<year <<" is not a leap year\n";
}
return 0;
}
Java program to check leap year
import java.util.Scanner;
public class LeapYearInJava {
public static void main(String[] args) {
int year;
Scanner stdin= new Scanner(System.in);
System.out.println( "Leap year program in java.\n Enter a year." );
year=stdin.nextInt();
if( year % 4 ==0 )
{
if( year % 100 == 0 )
{
if( year % 400 ==0 ) {
System.out.println(year+" is a leap year");
}
else {
System.out.println(year+" is not a leap year");
}
}
else
{
System.out.println(year+" is leap year");
}
}
else
{
System.out.println(year+" is c not leap year");
}
}
}