|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/h4sol/printLeaps.c
/*
printLeaps.c
You have two things to do to get this program to work.
1. You will take your code from leap.c in h3 and modify it so that the code that
calculates whether a year is leap or common is in a seperate function
2. Then, you are going to ask the user what year they were born in, and then
print all the leap years between (and including) your birth year and the
current year.
Main has already been started, but needs finished, and the function isLeap has
already been partially set up, it just needs the code to perform correctly.
*/
#include <stdio.h>
int isLeap(int); // function prototype
int main(){
int year, rt; //variable declarations
//asking the user what year they were born in
printf("What year were you born?> ");
scanf("%d", &year);
printf("The leap years since your birth were...\n");
/*
Set up the loop here
start with the year input by the user, loop until it reaches the current
year (2021).
inside the loop, you'll call "isLeap" with value "year" and put the
value into rt. isLeap returns 1 if the year is a leap year, and 0 if
it is a common year.
if the value of rt is 1, print just the year and a newline, otherwise
don't do anything
*/
for(int i = year; i <= 2021; i++){
rt = isLeap(i);
if(rt == 1)
printf("%d\n", i);
}
return 0;
}
//function definition
int isLeap(int year){
/*
Here, you will put the code that calculates whether or not a given year
is a leap year. Have the function return a 1 if the year given is a leap
year, or a 0 if it is a common year. Do NOT put any printf statements
inside the function.
*/
if(year % 4 != 0)
return 0;
else if(year % 100 != 0)
return 1;
else if(year % 400 != 0)
return 0;
else
return 1;
}
|