Leap Year Problems

This is a discussion on Leap Year Problems within the C Programming forums, part of the General Programming Boards category; Hey all, I have the following problem... "A leap year is any year divisible by 4, unless the year is ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    10

    Leap Year Problems

    Hey all,

    I have the following problem...

    "A leap year is any year divisible by 4, unless the year is divisible by 100 but not 400. Write a program to tell if a year is a leap year."

    I can use the following...
    printf
    sscanf
    fgets
    if
    else
    while
    break
    continue
    I can't use &&

    Basically I can use what you see below.

    So far I have this but I don't know what else to do. The following doesn't work. Basically I need help with what comes after the if (leap1 == 0) part

    Code:
    #include <stdio.h>
    
    char    line[10];
    int     year;
    int     leap1;
    int     leap2;
    int     leap3;
    
    int main()
    {
    printf("Enter the year to find out if it is a leap year: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    
    /*A leapyear is any year divisible by 4, unless the year is divisible by 100 but not 400.*/
    
    leap1 = year % 4;
    leap2 = year % 100;
    leap3 = year % 400;
    
    if (leap1 == 0)
    {
    printf("%d is a leapyear\n", year);
    }else{
    printf("%d is not a leapyear\n", year);
    }
    
    return(0);
    }
    Any help is greatly appreciated.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    use nested ifs to check all three conditions (simulating &&)
    Code:
    if (condition 1)
      if (condition 2)
        if (condition 3)
          then year is a leap year
    Last edited by ಠ_ಠ; 06-21-2009 at 02:45 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    ok, I did...
    Code:
    #include <stdio.h>
    
    char    line[10];
    int     year;
    int     leap1;
    int     leap2;
    int     leap3;
    
    int main()
    {
    printf("Enter the year to find out if it is a leap year: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    
    /*A leapyear is any year divisible by 4, unless the year is divisible by 100 but not 400.*/
    
    leap1 = year % 4;
    leap2 = year % 100;
    leap3 = year % 400;
    
    if (leap1 == 0)
     if (leap2 != 0)
      if (leap3 ==0)
    {
    printf("%d is a leapyear\n", year);
    }else{
    printf("%d is not a leapyear\n", year);
    }
    return(0);
    }
    ...but it is not working. I came across this (http://www.go4expert.com/forums/showthread.php?t=2099) which works and is similar to mine except it uses || and && which I can't use for this problem.
    Last edited by jc99; 06-21-2009 at 03:06 AM.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by jc99 View Post
    ok, I did...
    Code:
    #include <stdio.h>
    
    char    line[10];
    int     year;
    int     leap1;
    int     leap2;
    int     leap3;
    
    int main()
    {
    printf("Enter the year to find out if it is a leap year: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    
    /*A leapyear is any year divisible by 4, unless the year is divisible by 100 but not 400.*/
    
    leap1 = year % 4;
    leap2 = year % 100;
    leap3 = year % 400;
    
    if (leap1 == 0)
     if (leap2 != 0)
      if (leap3 ==0)
    {
    printf("%d is a leapyear\n", year);
    }else{
    printf("%d is not a leapyear\n", year);
    }
    return(0);
    }
    ...but it is not working. I came across this (Check for a leap year) which works and is similar to mine except it uses || and && which I can't use for this problem.
    you would need an else for every if, and would need to structure the messages properly, yours will only say it's a leap year when it's not divisible by 100 and is divisible by 400 (guess how many numbers satisfy that requirement (I'll give you a hint, don't divide by it))
    Last edited by ಠ_ಠ; 06-21-2009 at 03:20 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or you could combine all the sub-results into one expression, using && and || for "and" and "or".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by matsp View Post
    Or you could combine all the sub-results into one expression, using && and || for "and" and "or".

    --
    Mats
    Quote Originally Posted by jc99
    I can't use &&
    I think you missed this ridiculously stupid part
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    I have this and it works...
    Code:
    #include <stdio.h>
    
    char    line[10];
    int     year;
    int     leap1;
    int     leap2;
    int     leap3;
    
    int main()
    {
    printf("Enter the year to find out if it is a leap year: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    
    /*A leapyear is any year divisible by 4, unless the year is divisible by 100 but not 400.*/
    
    leap1 = year % 4;
    leap2 = year % 100;
    leap3 = year % 400;
    
    if (leap3 == 0 || (leap2 != 0 && leap1 == 0)){
    
    printf("%d is a leapyear\n", year);
    }else{
    printf("%d is not a leapyear\n", year);
    }
    return(0);
    }
    However it uses || and &&. I need to know how to write this program without those.

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by jc99 View Post
    I have this and it works...
    Code:
    #include <stdio.h>
    
    char    line[10];
    int     year;
    int     leap1;
    int     leap2;
    int     leap3;
    
    int main()
    {
    printf("Enter the year to find out if it is a leap year: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    
    /*A leapyear is any year divisible by 4, unless the year is divisible by 100 but not 400.*/
    
    leap1 = year % 4;
    leap2 = year % 100;
    leap3 = year % 400;
    
    if (leap3 == 0 || (leap2 != 0 && leap1 == 0)){
    
    printf("%d is a leapyear\n", year);
    }else{
    printf("%d is not a leapyear\n", year);
    }
    return(0);
    }
    However it uses || and &&. I need to know how to write this program without those.
    I told you, use an else for every if and interpret what it means if your program reaches it
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ಠ_ಠ View Post
    I think you missed this ridiculously stupid part
    Ok, I did miss that. So use some other method of nesting if-statements so that the printout only requires one set of if-statement. E.g. initialize a variable to "leapyear", use multiple if-statements to "not leapyear", then use a single if to determine whether the variable was set to "leapyear" or "not leapyear"

    Obviously "leapyear" and "not leapyear" probably should be the values 1 and 0 (or the other way around).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by matsp View Post
    Ok, I did miss that. So use some other method of nesting if-statements so that the printout only requires one set of if-statement. E.g. initialize a variable to "leapyear", use multiple if-statements to "not leapyear", then use a single if to determine whether the variable was set to "leapyear" or "not leapyear"

    Obviously "leapyear" and "not leapyear" probably should be the values 1 and 0 (or the other way around).

    --
    Mats
    or just have it print out "I'm a leap year"/"I'm not a leap year"

    @jc99
    200 is not a leap year but 400 is, you were interpreting it wrong in your code (it would still need to use else though)
    Last edited by ಠ_ಠ; 06-21-2009 at 03:37 AM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    Quote Originally Posted by ಠ_ಠ View Post
    I told you, use an else for every if and interpret what it means if your program reaches it
    How would that go? I did the following but it didn't work...

    Code:
    #include <stdio.h>
    
    char    line[10];
    int     year;
    int     leap1;
    int     leap2;
    int     leap3;
    
    int main()
    {
    printf("Enter the year to find out if it is a leap year: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    
    /*A leapyear is any year divisible by 4, unless the year is divisible by 100 but not 400.*/
    
    leap1 = year % 4;
    leap2 = year % 100;
    leap3 = year % 400;
    
    if (leap1 == 0)
    {
    } else if (leap2 != 0)
    {
    } else  if (leap3 == 0) {
    
    printf("%d is a leapyear\n", year);
    }else{
    printf("%d is not a leapyear\n", year);
    }
    return(0);
    }
    I think I need you to type out exactly what I need to do in the bolded part.

  12. #12
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by jc99 View Post
    How would that go? I did the following but it didn't work...

    Code:
    #include <stdio.h>
    
    char    line[10];
    int     year;
    int     leap1;
    int     leap2;
    int     leap3;
    
    int main()
    {
    printf("Enter the year to find out if it is a leap year: ");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &year);
    
    /*A leapyear is any year divisible by 4, unless the year is divisible by 100 but not 400.*/
    
    leap1 = year % 4;
    leap2 = year % 100;
    leap3 = year % 400;
    
    if (leap1 == 0)
    {
    } else if (leap2 != 0)
    {
    } else  if (leap3 == 0) {
    
    printf("%d is a leapyear\n", year);
    }else{
    printf("%d is not a leapyear\n", year);
    }
    return(0);
    }
    I think I need you to type out exactly what I need to do.
    Code:
    if (condition1)
      if (condition2)
        if (condition3)
           what does that mean 
        else
           what does that mean 
      else
        what does that mean 
    else
      what does that mean
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    10
    Quote Originally Posted by ಠ_ಠ View Post
    Code:
    if (condition1)
      if (condition2)
        if (condition3)
           what does that mean 
        else
           what does that mean 
      else
        what does that mean 
    else
      what does that mean
    I don't know how to translate that into actual code for my program, plus its late so I am going to bed. Thanks for the help anyways.

  14. #14
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by jc99 View Post
    I don't know how to translate that into actual code for my program
    that's pretty much the point of this pointless assignment, I don't expect anyone here to get more specific than that
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-05-2009, 05:42 AM
  2. Program showing if a year its leap year or not.
    By Cyberman86 in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2008, 08:00 AM
  3. What is one year of experience?
    By medievalelks in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-04-2008, 08:56 AM
  4. having problems with if and else statements
    By acohrockz21 in forum C Programming
    Replies: 10
    Last Post: 03-09-2008, 07:21 AM
  5. Help ...
    By Enoz28 in forum C Programming
    Replies: 4
    Last Post: 07-05-2004, 09:07 PM

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