why does this program say 1700 is a leap year?

This is a discussion on why does this program say 1700 is a leap year? within the C Programming forums, part of the General Programming Boards category; 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); ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    25

    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();
          }
    ok, so i've searched through prior threads about this topic, and i still have not been able to figure out an answer. thanks in advance for any help.

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    504
    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.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by newbcore View Post
    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
    Last edited by QuantumPete; 12-18-2008 at 09:06 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    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

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Thanks for the help, y'all. Much appreciated!



    Ok.
    Quote Originally Posted by stevesmithx View Post
    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.
    Last edited by newbcore; 12-19-2008 at 12:32 AM. Reason: spelling error

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    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.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Thanks, Adak. I'll keep those comments in mind.

  8. #8
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    504
    Quote Originally Posted by newbcore View Post
    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.
    And that's why I was trying to help you.
    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 View Post
    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]
    Last edited by stevesmithx; 12-19-2008 at 02:24 AM. Reason: add link
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Help with Day Month Year Program..due by 12
    By jgassen15 in forum C Programming
    Replies: 1
    Last Post: 12-06-2007, 11:21 PM
  3. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 08:14 AM
  4. a program to create months in a year newbie question
    By robasc in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 05:41 PM
  5. function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-02-2002, 08:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21