Sunday, March 13, 2011

C program to check whether a given year is leap year

Program
/* This program checks whether the given year is a leap year or not */
#include <stdio.h> /* It's ok not to have space between include and <stdio.h> */

int main (void) /* It's ok not to have space between main and (void). You can drop void off too. */
{
 int year;
 
 printf("\nEnter the year: ");
 scanf("%d",&year);
 
 if ( (year%4 == 0 && year%100 != 0) || year%400 == 0 )
  printf("\n%d is a leap year.\n\n",year);
 else
  printf("\n%d is not a leap year.\n\n",year);
 
 return; /* "return ;" works too. "return 0" works too. "return (0)" works too.*/ 
}
Compilation, Run and Output
[sreedhar@manchu2 cprogs_work_machine]$ gcc calendar.c -o calendar
[sreedhar@manchu2 cprogs_work_machine]$ ./calendar 

Enter the year: 2000

2000 is a leap year.

[sreedhar@manchu2 cprogs_work_machine]$ ./calendar 

Enter the year: 2001

2001 is not a leap year.

[sreedhar@manchu2 cprogs_work_machine]$ ./calendar 

Enter the year: 2004

2004 is a leap year.

[sreedhar@manchu2 cprogs_work_machine]$ ./calendar 

Enter the year: 2100

2100 is not a leap year.

[sreedhar@manchu2 cprogs_work_machine]$

No comments:

PBS Script Generator: Interdependent dropdown/select menus in Javascript

PBS SCRIPT GENERATOR
SH/BASH TCSH/CSH
Begin End Abort

About Me

LA, CA, United States
Here I write about the battles that have been going on in my mind. It's pretty much a scribble.

Sreedhar Manchu

Sreedhar Manchu
Higher Education: Not a simple life anymore