1.11M Members

leap year

 
0
 

Leap Years
Def: A year is a century year if it is divisible by 100.
Def: A year is a non-century year if it is not a century year.
Def: A year is a leap year if it is a non-century year that is divisible by 4, or a century year that is divisible by 400. Nothing else is a leap year.
In a source file named leapyears.cpp, write a program that will prompt the user for the starting year and ending year for a range of years and print to the screen all leap years in that range, 5 years per line. You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. Your main function will call the isLeapYear function in a loop for every year within the range delineated by, and including, the starting and ending years.

#include <iostream>
using namespace std;
 
bool LeapYear(int y)
{
return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0);
}
    
double isLeapYear(double x)
{
    if( x > -10000 && x < 10000)
    return x;
}
        
int main()
{
    int x;
    cout << "Please enter start and end of a range of years: ";
    cin >> x;
 if (isLeapYear(x))
    {
    if (LeapYear(x))
        cout << x;
    }
}

that's what i have, any ideas how to fix it?
thanks!

 
0
 

>>any ideas how to fix it?
fix what? you didn't say what's wrong with the code you posted.

why does isLeapYear() take a double as parameter? It should be an integer; afterall years do not contain fractions.

 
0
 

when i enter years, it doesn't return the one's that are leap years.

 
0
 

read the assignment again, and re-read it again and again until you understand it. IsLeapYear() is supposed to return TRUE if the year is a leap year or FALSE is the year is not a leap year. The function LeapYear() that you posted seems to do that. The functon IsLeapYear() that you posted does not do that, actually I have no clue what its purposes is. Delete IsLeapYear() and rename LeapYear() function to IsLeapYear().

In function main() you have to enter two years -- your program only gets one year. Then also in main() you need to create a loop that calls IsLeapYear() for each year between the first and second years that you enter from the keyboard.

Don't forget -- years prior to 1582 AD do not have leap years because the Gregorian calendar wasn't created until February 24, 1582.

 
0
 

You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. - not sure how
also not sure how to get it to print up to five leap years per line.

#include <iostream>
using namespace std;
    
bool isLeapYear(int y)
{
return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0);
}
     
int main()
{
    int x, z;
    cout << "Please enter start and end of a range of years: ";
    cin >> x >> z;
    
    for(int i = x; i <= z; i++)
    {
    if (isLeapYear(i))
    {
        cout << i << endl;
   }
    }
}
 
0
 

The function doesn't have to return a value to be able to display what the year is.

void leapYear(int year)
{
    if((year % 100) == 0 )
    {
        printf("%d is a century year\n", year);
    }
    else if(((year % 4) == 0) || ((year % 400) == 0))
    {
        printf("%d is a leap year\n", year);
    }
    else
    {
        printf("%d is a not century\n", year);
    }
}
 
0
 

>The function doesn't have to return a value to be able to display what the year is.
It does according to the assignment:

You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year.

Plus, as I see it, the program was supposed to be in C++, not C.

 
0
 
int leapYear(int year)
{
   return((!(year % 100 == 0))&&((year % 4 == 0) || (year % 400) == 0));
}

This will return if is a leap year or not.
call it inside a loop that will start with the first year and stop at the wanted one.
Inside the loop

if( leapYear( year ))
{
    printf("%d ", year);
}
 
0
 

You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. - not sure how
also not sure how to get it to print up to five leap years per line.

Looks like you have almost completed the assignment. To print 5 leap years per line I would add another counter in that loop to count the number of leap years printed on the line -- when the counter reaches 5 then print a "\n";

int counter = 0;
for(int i = x; i <= z; i++)
{
    if (isLeapYear(i))
    {
        cout << i << " ";
        if( ++counter == 5)
        {
            cout << "\n";
            counter = 0;
         }
     }
   }
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article