Monday, 2 July 2012

Checking for Leap Year in C


Code -


//______Program will find out whether a given year is leap year or not_________

/* An year is said to be a leap year if it is completely divisible by 400 OR it is divisible by 4 not by 100

Hint -  Modulus operator will be used (%)  */

//__________________Including Header Files___________________

#include<stdio.h>

#include<conio.h>

//________Mainly included for the getch() function____________

int main()

//________Program execution starts from the main function_____

{
int year;

printf("\n\n\t  ___________ Program to check year for being a Leap year ___________");

printf("\n\n Enter the year - ");

//___________Prompting the user to enter a year____________

scanf("%d", &year);

//__Reading the user input and storing that input in variable year of type Integer__

if (((year % 400) == 0) || (((year % 4) == 0) && ((year % 100) != 0)))

       //_____Checking the year for being a leap year______
{
printf("\n\n\t\t\t__________ %d is a leap year __________", year);

      //_____This statement will get executed if the year entered is a leap year_____

        }
else
{
printf("\n\n\t\t     _________ %d is not a leap year _________", year);

//_____This statement will get executed if the year entered is not a leap year_____

}

getch();

return 0;

       /*_return 0 tells the compiler that program has executed successfully without errors___*/

}

Follow us on Facebook - Assignment Hub

No comments:

Post a Comment