code not working


this program tells me thet evry year is a leep year


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
/*|||||||||||||||||||||||||||||||||||||||||*\
*this program inputs a value and checks     *
*to see if the input number is divisible    *
*by 4, 100 and makes sure its not divisible *
*by 400                                     *
*********************************************
*      *finds what year a leep year is*     *
*********************************************
* 0 = false, 1 = true                        *
\*|||||||||||||||||||||||||||||||||||||||||*/




#include<iostream>
using namespace std;

int main()
{
    int year;
    int Divisible_4    = 1;
    int Divisible_100  = 1;
    int Divisible_400  = 0;
    
    
    cout << " Welcome to the Leap Year Finder " << endl;
    cout << " Type in the year: ";
    cin  >> year;
    
    // checkes if years is divisible by 4
    if(year == year - (4 * year / 4))
    {
            Divisible_4 = 1;
    }
    else
    {
            Divisible_4 = 0;
    }
     // checkes if years is divisible by 100
    if(year == year - (100 * year/ 100))
    {
            Divisible_100 = 1;
    }
    else
    {
            Divisible_100 = 0;
    }
     // checkes if years is not divisible by 400
    if(year == year - (400 * year / 400))
    {
            Divisible_400 = 0;
    }
    else
    {   
            Divisible_400 = 1;
    }
    if(Divisible_4 == 1 && Divisible_100 == 1 && Divisible_400 == 0)
    {
         cout << " Yes this is a leap year " << endl;
    }
    else
    {    
         cout << " No this is not a leap year " << endl;
    }
    system("PAUSE");
    return 0;
}                   


I would like to know if it is my math or the if statments that stop it from working properly
(i have already tryed difining the varibles
Last edited on
maybe you should write:

 
if(year == (4 * (year / 4)))


anyway is there te module operator "%"
the module operator return the rest of the division.

Eg:
4%4 = 0
5%4 = 1
6%4 = 2
7%4 = 3
8%4 = 0

so you can write

1
2
3
if((year % 4) == 0) {
// it's a leep year
}


it dosent work it still returns that it is not a leep year


i have located were the problem is but not what


where:
1
2
3
4
5
6
7
8
if(Divisible_4 == 1 && Divisible_100 == 1 && Divisible_400 == 0)
    {
         cout << " Yes this is a leap year " << endl;
    }
    else
    {    
         cout << " No this is not a leap year " << endl;
    }



if i put in any number less then 400 it is wrong
Last edited on
I looked up the formula for a leap year and I think you're a little off with your formula. Any year that is divisible by 4 and not divisible by 100 is a leap year. Any year that is divisible by 100 and divisible by 400 is a leap year.

I made the requisite changes to your program and it worked (only tried a few years though).

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
int main()
{
    int year;
    int Divisible_4    = 1;
    int Divisible_100  = 1;
    int Divisible_400  = 0;


    cout << " Welcome to the Leap Year Finder " << endl;
    cout << " Type in the year: ";
    cin  >> year;

    // checkes if years is divisible by 4
    if(year % 4 == 0)
    {
            Divisible_4 = 1;
    }
    else
    {
            Divisible_4 = 0;
    }
     // checkes if years is divisible by 100
    if(year % 100 == 0)
    {
            Divisible_100 = 1;
    }
    else
    {
            Divisible_100 = 0;
    }
     // checkes if years is not divisible by 400
    if(year % 400 == 0)
    {
            Divisible_400 = 1;
    }
    else
    {
            Divisible_400 = 0;
    }
    if((Divisible_4 == 1 && Divisible_100 == 0) || (Divisible_100 == 1 && Divisible_400 == 1))
    {
         cout << " Yes this is a leap year " << endl;
    }
    else
    {
         cout << " No this is not a leap year " << endl;
    }
    system("PAUSE");
    return 0;
}
Last edited on
Yes of course, you should write

 
if(Divisible_4 == 1 || Divisible_100 == 1 || Divisible_400 == 0)


instead of

 
if(Divisible_4 == 1 && Divisible_100 == 1 && Divisible_400 == 0)


but finally you can just write

 
if(Divisible_4 == 1)


because every year Divisible_100 is also Divisible_4
and every year Divisible_400 is also Divisible_100 and Divisible_4
Last edited on
Years that are divisible by 100 are not leap years unless they are divisible by 400 according to the formula I read.
Sorry I didn't read that rule and I didn't know it.
It leaved me very perplexed but it's effectively right
I leaved 28 years before i have finally known that not all years divisible by 4 are leep year
:( sob!
Last edited on
I read the question wrong but i did get it to answer corrctly to what is told it to
the wrong thing
Last edited on
Topic archived. No new replies allowed.