20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The if Statement<br />

73<br />

To illustrate the use of a compound relational test <strong>in</strong> an actual program example,<br />

write a program that tests to see whether a year is a leap year. A year is a leap year if it is<br />

evenly divisible by 4.What you might not realize, however, is that a year that is divisible<br />

by 100 is not a leap year unless it also is divisible by 400.<br />

Try to th<strong>in</strong>k how you would go about sett<strong>in</strong>g up a test for such a condition. First,<br />

you could compute the rema<strong>in</strong>ders of the year after division by 4, 100, and 400, and<br />

assign these values to appropriately named variables, such as rem_4, rem_100, and<br />

rem_400,respectively.Then, you could proceed to test these rema<strong>in</strong>ders to determ<strong>in</strong>e if<br />

the desired criteria for a leap year are met.<br />

If you rephrase the previous def<strong>in</strong>ition of a leap year, you can say that a year is a leap<br />

year if it is evenly divisible by 4 and not by 100 or if it is evenly divisible by 400. Stop<br />

for a moment to reflect on this last sentence and to verify to yourself that it is equivalent<br />

to our previously stated def<strong>in</strong>ition. Now that you have reformulated our def<strong>in</strong>ition <strong>in</strong><br />

these terms, it becomes a relatively straightforward task to translate it <strong>in</strong>to a program<br />

statement as follows:<br />

if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 )<br />

pr<strong>in</strong>tf ("It's a leap year.\n");<br />

The parentheses around the subexpression<br />

rem_4 == 0 && rem_100 != 0<br />

are not required because that is how the expression will be evaluated anyway.<br />

If you add a few statements <strong>in</strong> front of this test to declare your variables and to enable<br />

the user to key <strong>in</strong> the year from the term<strong>in</strong>al, you end up with a program that determ<strong>in</strong>es<br />

if a year is a leap year, as shown <strong>in</strong> Program 6.5.<br />

Program 6.5 Determ<strong>in</strong><strong>in</strong>g if a Year Is a Leap Year<br />

// Program to determ<strong>in</strong>es if a year is a leap year<br />

#<strong>in</strong>clude <br />

<strong>in</strong>t ma<strong>in</strong> (void)<br />

{<br />

<strong>in</strong>t year, rem_4, rem_100, rem_400;<br />

pr<strong>in</strong>tf ("Enter the year to be tested: ");<br />

scanf ("%i", &year);<br />

rem_4 = year % 4;<br />

rem_100 = year % 100;<br />

rem_400 = year % 400;<br />

if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 )<br />

pr<strong>in</strong>tf ("It's a leap year.\n");

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!