why does this program say 1700 is a leap year?

• 12-18-2008
newbcore
why does this program say 1700 is a leap year?
Code:

```#include <stdio.h> main() {       int year;       printf("Enter a year\n");       scanf("%d", &year);       getchar();             if (year%400==0)       printf("%d = leap year", year);                 else if((year%100==0)&&(year%400==0))                 printf("%d = leap year", year);                 else if(year%4==0)                 printf("%d = leap year", year);                 else                 printf("%d = not a leap year", year);                   getchar();       }```
• 12-18-2008
stevesmithx
1700 is divisble by 4.
If you trace through the code manually or a debugger
you can easily figure that out.
Also please indent properly and main returns int.
• 12-18-2008
QuantumPete
Quote:

Originally Posted by newbcore
Code:

```                else if(year&#37;4==0)                 printf("%d = leap year", year);```

1700 is divisible by 4 if I'm not mistaken. That's why your code reports it as a leap year.

QuantumPete
• 12-18-2008
samf
You may want to try this:

Code:

```\$ cat isleap.c #include <stdio.h> #include <stdlib.h> #define NUMYEARS 14 int main () {  int year[NUMYEARS] = {1700, 1900, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008};  int i;  for (i = 0; i < NUMYEARS; i++) {     if ( (year[i] % 4 == 0 && year[i] % 100 !=0) || (year[i] % 400 == 0) )  {         printf("%d = leap year\n", year[i]);     }     else {       printf("%d = not a leap year\n", year[i]);     }  }  exit (0); } \$ gcc -Wall isleap.c \$ a.out 1700 = not a leap year 1900 = not a leap year 1996 = leap year 1998 = not a leap year 1999 = not a leap year 2000 = leap year 2001 = not a leap year 2002 = not a leap year 2003 = not a leap year 2004 = leap year 2005 = not a leap year 2006 = not a leap year 2007 = not a leap year 2008 = leap year```
• 12-18-2008
newbcore
Thanks for the help, y'all. Much appreciated!

Ok.
Quote:

Originally Posted by stevesmithx
1700 is divisble by 4.
If you trace through the code manually or a debugger
you can easily figure that out.
Also please indent properly and main returns int.

I am certain you already know this, but I do feel compelled to mention it.

It's obvious that I couldn't figure it out. It's also obvious that I don't know what "indenting properly" actually is, or what "main returns int" means. I am trying to learn.

Do you think I'm trying to... I don't even know. Manipulate you? Don't feed me a line about laziness. What I posted above took me hours, however pathetic that may be to you.
• 12-18-2008
Newbcore, we've all been there. Don't take any brusque replies personally. It's the nature of forums inability to "talk", at a decent speed, and very common on the internet.

Nobody's taking aim at you because you're a dumb programmer - because we were ALL dumb programmers, and some of us <clears throat> ain't too far from it, yet. :p :p

If you can get a "words roll off me like water off a duck's back", kind of attitude going, you'll learn a lot, a lot quicker. And don't worry, when the word's not rolling off your back, we got your back, OK?

What you may not know is all programmers have developed a visual acuity, just by programming so much, that indentation of another sort, just murders. You can go from running at top speed, scanning down a program's code, to feeling like you're running in quicksand. Uhh!

You'll get that way too, if you are actively programming, even as a hobby.
• 12-18-2008
newbcore
• 12-19-2008
stevesmithx
Quote:

Originally Posted by newbcore
It's obvious that I couldn't figure it out. It's also obvious that I don't know what "indenting properly" actually is, or what "main returns int" means. I am trying to learn.

The point of these forums is to not only to help you program but also to write good portable programs.
When I started to learn C by posting here at this forum I made these same mistakes and I was told to correct them by the experts here (in fact,a bit harshly sometimes)and I took their suggestions.
Indenting Properly
main returns int

If you are not sure about a term or jargon just google it and you'll usually get what you want.

Quote:

Originally Posted by newbcore
Do you think I'm trying to... I don't even know. Manipulate you? Don't feed me a line about laziness. What I posted above took me hours, however pathetic that may be to you.

I never told that you were trying to.
I don't know about you ,but my laziness and mediocre typing speed doesn't permit me to write "No offence intended" at the end of every post I am replying to. :)
Finally, learn to have a thicker skin at the online forums because you are going to need it. [No offence intended] ;)