Little help with leap years

This is a discussion on Little help with leap years within the C Programming forums, part of the General Programming Boards category; Ok, a leap year is any year divisible by 4 with the exception of any year divisible by 100 but ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14

    Little help with leap years

    Ok, a leap year is any year divisible by 4 with the exception of any year divisible by 100 but not 400. I'm writing a program that will let the user input a year and it will output whether or not that year is a leap year. I have most of the code already written and don't really need help with that. I am a bit confused on how to write the actual calculating part itself though. Do you think it would be easier to use a switch or if/else ?

  2. #2
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    uhmm, why dont you post your code so the C elites would know what advice to give best
    It is not who I am inside but what I do that defines me.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14
    Well, here's what I got so far. Don't think it'll help much, since I've been waiting to figure out what is the best way to calculate if it's a leap year or not.

    Code:
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	// Display Instructions
    	printf("Leaps years can be defined as any year divisible by 4 (except any year divisible by 100 but not 400. Enter a year below to find out if it if a leap year.");
    
    	//Input: year
    	int year;
    	printf("Enter the year:");
    	scanf("%d", &year);
    
    	//Figure out whether the year is a leap year or not.
    	
    
    	return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Do you think it would be easier to use a switch or if/else ?
    Well, an if, seeing as how it's as simple as:
    Code:
    if ( <leap year expression> ) {
      /* Leap year */
    }
    else {
      /* Not leap year */
    }
    The expression is straightforward, and a direct translation of how it's described.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14
    I'm sorry, I'm still kinda new to all this. I think I'm just being retarded, but what would be an expression to use for leap year expression? I just can't think of how to write it into a formula.

  6. #6
    Me
    Join Date
    Jul 2006
    Posts
    71
    I will give you a hint. You use the modulo operator.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm sorry, I'm still kinda new to all this.
    Which is why we're going out of our way not to give you the answer straight out. You learn more if you figure it out on your own. But here's a more formal description:

    If year is divisible by 4 and year is not divisible by 100 or year is divisible by 400. See what you can come up with.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    The modulous operator returns the reamainder of a devision problem
    it is % - SHIFT 5
    so you would say
    if (any year%4==0) then it is a leap year.. it can be devided evenly by 4

  9. #9
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14
    Ok, I think I got it. I wasn't sure exactly how to write out like "if year is divisible by 4" ect., but I think I got it now. Here is what I came up with
    Code:
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	// Display Instructions
    	printf("Leap years can be defined as any year divisible by 4 (except any year divisible by 100 but not 400. Enter a year below to find out if it if a leap year.");
    
    	//Input: year
    	int year;
    	printf("\n\nEnter the year:");
    	scanf("%d", &year);
    	fflush(stdin);
    
    	//Figure out whether the year is a leap year or not.
    	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
    		printf("\n%d is a Leap Year.", year);
    	else
    		printf("\n%d is not a Leap Year.", year);
    
    	getchar ();
    	return 0;
    }
    Thanks for the help Prelude, Kivork, and relyt_123. You guys helped me out a bunch. Atleast I now know a little bit more about programming.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > printf("Leap years can be defined ..
    > int year;
    In C, all declarations must come before statements.
    Check you're not using a C++ compiler to compile C code.


    > fflush(stdin);
    This is very poor - input streams cannot be flushed
    See the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Also for anti-ambiguity reasons (just to reaffirm the order) perhaps you should add an extra set of paranthisis in your if statement.

    Code:
    if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
    // to
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Pretty sure there's supposed to be a #include <stdio.h> in there somewhere.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  13. #13
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    Code:
     #include "stdafx.h"
    hmmm, i think this custom header includes the necessary standard headers...
    It is not who I am inside but what I do that defines me.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    Not on my machine it doesn't.
    Nor does it understand that _tmain() stuff either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    hehehe, i guess im wrong then..
    It is not who I am inside but what I do that defines me.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Leap Year Problems
    By jc99 in forum C Programming
    Replies: 13
    Last Post: 06-21-2009, 05:02 AM
  2. why does this program say 1700 is a leap year?
    By newbcore in forum C Programming
    Replies: 7
    Last Post: 12-19-2008, 02:03 AM
  3. Help with leap year program.
    By IxTanGxI in forum C Programming
    Replies: 8
    Last Post: 02-20-2006, 08:49 PM
  4. Working out leap years.
    By parnaby in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2006, 08:41 AM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 12:56 PM

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