Write a program to check if a given year is a leap year or not.
Leap Year: A year is a leap year if it is divisible by 4. If a year is a century year then, if the year is divisible by 400 then it is a leap year, otherwise not a leap year.
Example 1:
Input :2020
Output: Leap Year
Example 2:
Input :2010
Output: Not a Leap Year
Approach
C
#include <stdio.h>#include <stdbool.h>//function to check given//year is leap or notbool isLeapYear(int year){//if year%100==0if (year % 100 == 0){//if year%400==0if (year % 400 == 0)return true;elsereturn false;}//if year%4!=0if (year % 4 != 0)return false;elsereturn true;}int main(){int year = 2020;if (isLeapYear(year)){printf("Leap Year\n");}else{printf("Not a Leap Year\n");}return 0;}
Java
public class LeapYear {public static void main(String[] args) {int year = 2020;if (isLeapYear(year)) {System.out.println("Year is leap year");} else {System.out.println("Year is not leap year");}}private static boolean isLeapYear(int year) {if (year % 100 == 0) {if (year % 400 == 0)return true;elsereturn false;}if (year % 4 == 0) {return true;}return false;}}
C++
#include <bits/stdc++.h>using namespace std;//function to check given//year is leap or notbool isLeapYear(int year){//if year%100==0if(year%100==0){//if year%400==0if(year%400==0)return true;elsereturn false;}//if year%4!=0if(year%4!=0)return false;elsereturn true;}int main(){int year=2020;if(isLeapYear(year))cout<<"Leap Year\n";elsecout<<"Not a Leap Year\n";return 0;}
No comments:
Post a Comment