Trying to improve the leap year algorithm

I am trying to improve the leap year algorithm to tell me when the next leap year is if the year entered was not a leap year. I am still new to c++ and this is what I have so far.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>		// Access output stream

using namespace std;       // Access cout, endl, cin

bool IsLeapYear(int);	// Prototype for subalgorithm	
bool LeapYear1(int);
bool LeapYear2(int);
bool LeapYear3(int);
bool LeapYear4(int);


int main()
{
	int year;			// Year to be tested
	cout << "Enter a year AD, for example, 1997."
		<< endl;  		// Prompt for input
	cin >> year;      	// Read year

	if (IsLeapYear(year))       	// Test for leap year
		cout << year << " is a leap year." << endl;
	
	else
		cout << year << " is not a leap year." << endl;
	
	if (LeapYear1(year))       	// Test for leap year
		cout << year << " is the next leap year." << endl;

	else if (LeapYear2(year))
		cout << year << " is the next leap year." << endl;

	else if (LeapYear3(year))
		cout << year << " is the next leap year.";
	else if (LeapYear4(year))
		cout << year << " is the next leap year.";
	
	system("pause");
	return 0;
}
//******************************************************
bool IsLeapYear(int year)
// IsLeapYear returns true if year is a leap year and
// false otherwise
{
	if (year % 4 != 0)        // Is year not divisible by 4?
		return false;         // If so, can't be a leap year
	else if (year % 100 != 0) // Is year not a multiple of 100?
		return true;          // If so, is a leap year
	else if (year % 400 != 0) // Is year not a multiple of 400?
		return false;	     	// If so, then is not a leap year
	else
		return true;        	// Is a leap year
}
bool LeapYear1(int year)
{
	if ((year + 1) % 4 != 0)
		return false;
	else if ((year + 1) % 100 != 0)
		return true;
	else if ((year + 1) % 100 != 0)
		return false;
	else
		return true;
}
bool LeapYear2(int year)
{
	if ((year + 2) % 4 != 0)
		return false;
	else if ((year + 2) % 100 != 0)
		return true;
	else if ((year + 2) % 100 != 0)
		return false;
	else
		return true;
}
bool LeapYear3(int year)
{
	if ((year + 3) % 4 != 0)
		return false;
	else if ((year + 3) % 100 != 0)
		return true;
	else if ((year + 3) % 100 != 0)
		return false;
	else
		return true;
}
bool LeapYear4(int year)
{
	if ((year + 4) % 4 != 0)
		return false;
	else if ((year + 4) % 100 != 0)
		return true;
	else if ((year + 4) % 100 != 0)
		return false;
	else
		return true;
	return 0;
} 
Last edited on
Please edit your post to include [code][/code] tags around the code (the <> format).
I fixed it sorry.
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
#include <iostream>

const int GREGORIAN_START = 1583 ; // first full year of the gregorian calendar

bool IsLeapYear(int year)
// IsLeapYear returns true if year is a leap year and
// false otherwise
{
    if( year < GREGORIAN_START ) return false ; // year before gregorian calendar

    if (year % 4 != 0)        // Is year not divisible by 4?
        return false;         // If so, can't be a leap year
    else if (year % 100 != 0) // Is year not a multiple of 100?
        return true;          // If so, is a leap year
    else if (year % 400 != 0) // Is year not a multiple of 400?
        return false;	     	// If so, then is not a leap year
    else
        return true;        	// Is a leap year
}

int NextLeapYear( int year )
{
    if( year < GREGORIAN_START ) year = GREGORIAN_START ;

    // increment year till we get to the next leap year (brute force)
    do ++year ; while( !IsLeapYear(year) ) ;

    return year ;
}

int main()
{
    int year;			// Year to be tested
    std::cout << "Enter a year AD, for example, 1997: " ;
    std::cin >> year;      	// Read year

    if( IsLeapYear(year) ) std::cout << year << " is a leap year.\n" ;
    else std::cout << year << " is not a leap year.\n" ;

    std::cout << "the next four leap years are: " ;
    for( int i = 0 ; i < 4 ; ++i )
    {
        year = NextLeapYear(year) ;
        std::cout << year << ' ' ;
    }
    std::cout << '\n' ;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

bool IsLeapYear(int y)
{
    return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
}

int main()
{
    int year;
    std::cout << "Enter a year: ";
    std::cin >> year;

    if (IsLeapYear(year))
        std::cout << year << " is a leap year.\n";
    else
    {
        std::cout << year << " is not a leap year.\n";
        while (!IsLeapYear(++year)) ;
        std::cout << year << " is the next leap year.\n";
    }
}

Last edited on
Topic archived. No new replies allowed.