Showing posts with label leap year. Show all posts
Showing posts with label leap year. Show all posts

C and C++ Program to Check Whether a Given Year is Leap Year or Not

This is post contains a C program and a C++ program to check whether an input year is leap year or not. A year is a leap year if:

  1. The year is divisible by 400
  2. The year is divisible by 4 but not divisible by 100.
Any other year is not a leap year.

C program
#include <stdio.h>
void main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if(year%400==0 || ((year%4=0) && (year%100!=0)))
printf("%d is a leap year.\n", year); else
printf("%d is not a leap year.\n", year);
}


C++ program
#include <iostream.h>
void main()
{
int year;
cout<<"Enter a year to check if it is a leap year or not";
cin>>year;
if(year%400==0 || ((year%4=0) && (year%100!=0)))
cout<<"\n"<<year<<"is a leap year."; else
cout<<"\n"<<year<<"is NOT a leap year."; 
}