Help with leap year program.

This is a discussion on Help with leap year program. within the C Programming forums, part of the General Programming Boards category; I am making a martian leap year program(pointless program for class). This is the given : 1) All odd years ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    23

    Thumbs up Help with leap year program.

    I am making a martian leap year program(pointless program for class). This is the given :

    1) All odd years are leap years.
    2) All years divisible by 8, but not by 32 are leap years. (Thus, 8, 16 and 24 are leap years, but 32 is not. Similarly 40, 48 and 56 are leap years but 64 isn't.)

    I made a double if statment but it is not working. When I just had part 2 it worked fine but when I added the odd years it is not working.

    Code:
    #include <stdio.h>
    
    
    int main() {
    
    	int year;
    	
    	printf("Please enter a martian year\n");
    	scanf("%d", &year);
    	/* leap years occur if: years can be divided by 8 evenly and not by 32*/
    	if (year%8 == 0 && year%32 != 0){
    		if(year%2 == 1)
    			printf("\n%d is a leap year", year);
    		}
    		else
    			printf("\n%d is not a leap year", year);
    	
    	
    	
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    Watch your indentation!

    Code:
    if (year%8 == 0 && year%32 != 0){
    	if(year%2 == 1)
    		printf("\n%d is a leap year", year);
    }
    else
    	printf("\n%d is not a leap year", year);

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    I did not think indention would make a difference when it runs. I tried it anyways and it did not work.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Watch your if statements. Your program is making the assumption that all odd numbers are divisible by 8 and not divisible by 32. Each of your conditions are independant of each other. By nesting your statements, you make the interior conditions dependant on the exterior.
    Last edited by SlyMaelstrom; 02-20-2006 at 04:08 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    Alright, I actually didn't read your post very thoroughly... :P But seriously, all odd years are leap years? Are we talking leap years as in an extra day in February? 'Cause I thought that was something that occured every fourth year, with the exception of some simple algorithm which I guess you've layed out above.

    Anyhow, you need to rethink the logic in your program. What you're saying above is something like "if divisible by 8 AND not divisible by 32 AND odd --> it's a leap year".
    Last edited by tretton; 02-20-2006 at 04:10 PM. Reason: Grammar.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Tretton, you don't look very smart when you reply to a post without reading it. The program gets "Martian leap years". Now please, read... then answer.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    Yes, you are correct. My brain isn't working tonight. I rest my case.

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Quote Originally Posted by IxTanGxI
    I did not think indention would make a difference when it runs. I tried it anyways and it did not work.
    It doesn't make a difference.

    However, when you post code, the indentation usually tells us what you intend to be happening. In your case:
    Code:
    if (year%8 == 0 && year%32 != 0){
    		if(year%2 == 1)
    			printf("\n%d is a leap year", year);
    		}
    		else
    			printf("\n%d is not a leap year", year);
    Because of where you've put the closing brace } it makes the else bound to the if (year%8... and NOT the if (year%2 which is what your intentation appears to suggest. So, with correct indentation (but the same code), it would look like as per tretton's first post.

    If you actually want to do what your indentation suggests, it should be instead:
    Code:
    if (year%8 == 0 && year%32 != 0) {
        if (year % 2 == 1)
            printf("\n%d is a leap year", year);
        else
            printf("\n%d is not a leap year", year);
    }

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    if ( year % 2 == 1 || (year % 8 == 0 && year % 32 != 0) )
    [edit]An odd year or a year evenly divisible by 8 and not evenly divisible by 32.
    1) All odd years are leap years.
    2) All years divisible by 8, but not by 32 are leap years.
    Last edited by Dave_Sinkula; 02-20-2006 at 07:59 PM. Reason: Added explanation and color.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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, 01: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, 10:21 PM
  3. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07: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, 04:41 PM

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