1.11M Members

Beginners Program: Leap Year

 
0
 
/* Any year is input through the keyboard. write a program to determine
   whether the year is a leap year or not. */

#include <stdio.h>
#include <conio.h>

void main(void)
{
 int year;
 clrscr();
 printf("\n\n      enter a year::");
 scanf("%d",&year);

 if(year%400==0)
    printf("\n\n         Leap Year.");
 else 
    if(year%100==0)
    printf("\n\n         Not Leap Year.");
 else 
    if(year%4==0)
    printf("\n\n         Leap Year.");
 else
    printf("\n\n         Not Leap Year.");
 
 getch();
}

Is this Program Correct? or is there more conditions to check..?

 
0
 

Hello Sneha,
Doing some quick google search, I couldn't find any more conditions.
So I guess that's it :)

 
3
 

>Is this Program Correct? or is there more conditions to check..?
No and no, respectively. Your algorithm for the leap year condition is correct, but your code has a little to be desired.

>#include <conio.h>
This header is completely unnecessary for this program. I highly recommend you get out of the habit of including unnecessary non-portable libraries.

>void main(void)
main has returned int for about 60 years. It looks like this:

int main(void)
{
  /* Your code here */

  return 0;
}

Or if you're accepting command line parameters, this:

int main(int argc, char *argv[])
{
  /* Your code here */

  return 0;
}

Nothing else is acceptable if you're a beginner.

>clrscr();
I hate this for two reasons:

  1. clrscr is not a portable function, which means you've limited your code to the compiler that it was written with. Further, clrscr is unnecessary in this situation, so you've needlessly destroyed portability.
  2. Clearing the screen at the beginning of the program is either pointless or antisocial. If you're running the program from an IDE, the output window is owned by the IDE and nothing will precede your first explicit output anyway. If you're running from the command line, removing the entirety of output from previous programs will piss off damn near everyone who uses your program.

>printf("\n\n enter a year::");
While I'm sure it works fine on your compiler and system, output is only guaranteed to be displayed immediately if you print a newline at the end of what you want displayed, flush the stream explicitly using the fflush function, or the output buffer is conveniently filled up. The latter situation shouldn't be relied upon, so you have two options:

  1. If a line break is acceptable in the output, print a newline:
    printf("\n\n      enter a year::\n");
  2. If a line break is not acceptable in the output, call fflush:
    printf("\n\n      enter a year::");
    fflush(stdout)

This is best practice, I recommend you adhere to it unless there's good cause to deviate.

>scanf("%d",&year);
The vast majority of functions will give you some indication of success or failure. Especially with input it's best to make sure that the call succeeded before proceeding as if it did. In the case of scanf, a successful call will return the number of items converted:

if (scanf("%d",&year) == 1) {
  /* Success */
}
else {
  /* Failure */
}

>getch();
This is another pattern that's unnecessary, but I'll refrain from blasting you for it because the standard alternative isn't quite as simple and requires an intermediate understanding of how I/O streams work.

Here's an example that corrects everything I've mentioned:

#include <stdio.h>

void jsw_flush(FILE *in)
{
    int ch;

    do
        ch = getc(in);
    while (ch != '\n' && ch != EOF);

    clearerr(in);
}

int main(void)
{
    int year;

    printf("Enter a year: ");
    fflush(stdout);

    if (scanf("%d", &year) == 1) {
        if (year % 400 == 0)
            printf("Leap Year\n");
        else if (year % 100 == 0)
            printf("Not Leap Year\n");
        else if (year % 4 == 0)
            printf("Leap Year\n");
        else
            printf("Not Leap Year\n");
    }

    jsw_flush(stdin);
    getchar();

    return 0;
}
 
0
 

well.. i don't think there is anything left to comment but there is so many things to learn (specially for me) from the above post.. :)

 
0
 

OMG. So many mistakes in my single program. I never heard about such things. Neither from my college teacher nor from books..

 
0
 

Still few points are going over my head. But i already note down everything and i will try to learn.

Question Answered as of 3 Years Ago by Narue, Hyperion and vinitmittal2008
 
1
 

nice but u use \n\n more time this is wrong because that have many space occupied.

 
0
 

Very very and very informative.i learned so many new things here...all can say is ''LOGIC is very important''... Thanks all

 
0
 

what if a negative year is inputed? the program doesn't work

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article