using division with in a else if statement

I need to be able to make a leap year calculater using a % function with in the if else statement or switch statement (the later im not as fimilar with). Im sorry if this has already been posted i did a search and didnt come back with any thing so i appoligize in advance. the code is as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>

int main ()
{
//the way to find out if a year was a leap is the year should be dividable by 4
//but not by 100 and it can be dividable by 400
int LeapYear = 0;
int Leap400 = 400;
int Leap4 = 4;
int Leap100 = 100;

//intial statement asking for input
printf("please enter year to find out if it was a leap year \n")
gets(&LeapYear);

//finds out if the year is divisable by both 100 and 4
if ((LeapYear % Leap4 == 0) || (LeapYear % Leap100 == 0))

printf ("this year was not a leap year");

//finds out if the year is divisable by 4 but not 100
else if ((LeapYear % Leap4 == 0) || (LeapYear % Leap100 != 0))

printf ("this year was a leap year");

//finds out if year is divisable by 400
else if (LeapYear % Leap400 == 0)

printf ("this year was a leap year");

else 

printf ("this year was not a leap year");

//complier specific pause so user can see output
system ("pause");

return 0;
}


(To check your work, legal leap years are 1952, 1964, 1988, and 2004. Years 1953, 1966, 1990, and 2007 are not leap years.) this is something that was included to help check work.
Last edited on
I found the answer to my own question here it is for any one with similiar problems....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main ()
{
//the way to find out if a year was a leap is the year should be dividable by 4
//but not by 100 and it can be dividable by 400
int LeapYear;
int Leap400 = 400;
int Leap4 = 4;
int Leap100 = 100;
char szInput [256];

//intial statement asking for input
printf ("please enter year to find out if it was a leap year \n");

//used fgets becuase it allows for this type of input
fgets(szInput, 256, stdin);

//used atoi becuase it converts the szInput back to int value so i can 
//in  turn use it for what i needed it to do
LeapYear = atoi (szInput);

//used this to make sure atoi was doing what i needed it to do
printf ("this is the year you entered %d \n", LeapYear);

//finds out if the year is divisable by 4
if (fmod (LeapYear,Leap4) == 0)

printf ("this year was a leap year \n");

//finds out if year is divisable by 400
else if (fmod (LeapYear,Leap400) == 0)

printf ("this year was a leap year \n");

//finds out if the year is divisable by both 100 and 4
else if ((fmod (LeapYear,Leap4) == 0) || (fmod (LeapYear,Leap100) == 0))

printf ("this year was not a leap year \n");

//and of course the catch all
else 

printf ("this year was not a leap year \n");

//complier specific pause so user can see output
system ("pause");

return 0;
}


i used fmod becuase it allows me to find the reminder between to numbers or in this case one constant and one variable entered by the user. ill probbly add a code to it to allow make sure users enter the full year to prevent errors.
Last edited on
Topic archived. No new replies allowed.