Click here to Skip to main content
15,912,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to create three Boolean variables for this but I'm confused on how to do so.

Example Output 1 (not a leap year, input is in bolded italics):
Enter a year: 1800
Is the year evenly divisible by 4? Yes.
Is the year evenly divisible by 100? Yes.
Is the year evenly divisible by 400? No.
1800 is not a leap year.


Example Output 2 (is a leap year, input is in bolded italics):
Enter a year: 2364
Is the year evenly divisible by 4? Yes.
Is the year evenly divisible by 100? No.
Is the year evenly divisible by 400? No.
2104 is a leap year.

What I have tried:

This is what I have so far. But I don't feel like I'm on the right track.

C++
 #include <iostream>
#include <string>
using namespace std;

int main ()
{
    int year;

    cout << "Enter a year: ";
    cin >> year;
    cout << endl;
    
    if (leap_year(year) == true)
    {
        cout << "Is the year evenly divisible by 4? Yes." 
    }
    if (leap_year(year) == false)
    {
        cout << "Is the year evenly"
    }
        
return 0;
}</string></iostream>
Posted
Updated 25-Feb-16 1:17am
v2
Comments
Patrice T 23-Feb-16 6:17am    
You certainly can do better than this.
If you can't look for another job than programmer.
You should be able to get the right code for divisible by 4, 100, 400 at least.
[no name] 23-Feb-16 6:23am    
I suggest you to read about modulus Operator "%". It will help you for all your questions you listed.

Write your function leap_year so that it returns a bool value.
Inside the function, do the first test. If it fails, return false.
Do the second test. If it fails, return false.
Do the final test. If it fails, return false.
Otherwise, return true.
In your main function, change the code to:
C++
if (leap_year(year))
{
    cout << year << " is a leap year.";
}
else
{
    cout << year << "Is not a leap year."
}
 
Share this answer
 
You must put some logic in your leap_year function. I have read the wikipedia for it. May it is right

C++
bool leap_year(int year)
{
//never a leap year
 if( (year % 4) != 0) return false;

  if( (year % 100) != 0) return true;
  //the big case
  if( (year % 400) == 0) return false;

  return true;
}


tip: write test functions like
C++
ASSERT( leap_year(1980) );//is leap year
ASSERT( !leap_year(1981) );//not leap year
 
Share this answer
 
Comments
Dave Kreskowiak 23-Feb-16 8:35am    
This code won't produce the output he's looking for. In any case, do NOT do his homework for him.
You can try this to check year is leap year or not.

C++
int year;
if((year % 400 == 0) || ((year % 4 == 0)&&(year % 100 != 0)))
    return true;  // year is leap year
else
    return false;  // not a leap year



Condition for leap year.
*if year is completely divided by 400.
OR
*if year is completely divisible by 4 but not by 100.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900