Leap Year Problems

• 06-21-2009
jc99
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.
• 06-21-2009
ಠ_ಠ
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```
• 06-21-2009
jc99
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.
• 06-21-2009
ಠ_ಠ
Quote:

Originally Posted by jc99
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))
• 06-21-2009
matsp
Or you could combine all the sub-results into one expression, using && and || for "and" and "or".

--
Mats
• 06-21-2009
ಠ_ಠ
Quote:

Originally Posted by matsp
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
• 06-21-2009
jc99
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.
• 06-21-2009
ಠ_ಠ
Quote:

Originally Posted by jc99
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
• 06-21-2009
matsp
Quote:

Originally Posted by ಠ_ಠ
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
• 06-21-2009
ಠ_ಠ
Quote:

Originally Posted by matsp
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)
• 06-21-2009
jc99
Quote:

Originally Posted by ಠ_ಠ
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.
• 06-21-2009
ಠ_ಠ
Quote:

Originally Posted by jc99
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```
• 06-21-2009
jc99
Quote:

Originally Posted by ಠ_ಠ
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.
• 06-21-2009
ಠ_ಠ
Quote:

Originally Posted by jc99
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