Programming Resource
  leap.c
 
/* Filename     :   leap.c
* Function Name: main
* Written By : Prof. Kantila [kantila@gmail.com]
* Dated : 11th Dec 2006
* Modified on : 18th Dec 2006
* Purpose : Program to find out leap year in given range
* Arguments : None, Ask interactively
* Header files : stdio.h, leap.h
*/
#include <stdio.h> /*Standard Input/Output functions required */
#include "leap.h" /*leapf function is defined in this file */

#define COLS 5 /*Defining number of columns to be displayed 1-8*/
/*main function, return integer but takes no argument*/
int main(void)
{
/*Declaration of local variables to hold data */
long int y1=0,y2=0,current,max,counter=0;
/*Using unformated output function fputs, it takes two arguments,
first is string and second in of type FILE *, here stderr*/
fputs("This will display the leap years in the given range...\n",stderr);
/*Using formatted output function fprintf, it takes one more argument
than printf as the first argument of the type FILE *, here stderr*/
do
{
fprintf(stderr,"Input first year of range [1 - 999999999] : ");
/*Using formatted input function fscanf to read an integer value from
standard input stream, i.e. stdin - it also takes one more argument than
scanf function as the first argument of type FILE * */
fscanf(stdin,"%9li",&y1);
if(y1<=0)
fprintf(stderr,"Invalid year!\a\a\n");
/*fflush(FILE *) function clears (flushes) the specified stream
This is used here to remove any unwanted data (garbage) from stdin stream*/
fflush(stdin);
} while(y1<=0);
while(1)
{
fprintf(stderr,"Input last year of range [1 - 999999999] : ");
fscanf(stdin,"%9li",&y2);
if(y2<=0)
fprintf(stderr,"Invalid year!\a\a\n");
else
break;
fflush(stdin);
/*Conditional operator is used to assign smaller year value to current
and larger one to max variable so that years can be displayed in increasing
order */
}
(y1<y2)?(current=y1,max=y2):(current=y2,max=y1);
/*Displaying message on standard output terminal - it may be redirected */
fprintf(stdout,"Following are the leap years in the given range...\n");
/*Use of do ... while loop */
do
{ /*Body of do loop begins here*/
/*Testing wheather given year is a leap year or not*/
if(leapf(current))
{ /*Body of if true condition begins here*/
fprintf(stdout,"%10li",current); /* %8i here ensures to print the output
* in at least 8 columns */
/*Using counter to count the number of columns displayed */
counter++;
/*Testing wheather the columns displayed are equal to the specified */
if(counter==COLS)
{
/*If specified columns have been displayed, move to next line */
printf("\n");
/*Reset the column counter to 0 */
counter=0;
}
}
current++; /*Increment to year*/
} /*Body of the do loop ends here */
while(current<=max); /*Condition of do...while loop is specified here*/
/*If the above condition is still true, do body will execute once more
else the control will move to next line*/
fprintf(stderr,"\nLeap years has been displayed successfully.\n");
/*Above message will go to monitor - even if output is redirected
because it is being send to standard error terminal not standard output*/
return 0; /*returning control back to Operating System */
}





 
 
  Today, there have been 6 visitors (6 hits) on this page!  
 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free