Search code examples
c++leap-year

Leap year exercise. but "else" has to print how many years there is left to next leap year


So I have almost just started coding. And I wondered if any of you guys could help me with this simple code. I need to state how many years there is left till next leap year and I am lost.

int main()
{

int year;

cout << "Enter a year: ";
cin >> year;

if (year % 4 == 0)
{
    if (year % 100 == 0)
    {
        if (year % 400 == 0)
            cout << year << " is a leap year.";
        else
            cout << "There is " << 4%-year << " years till next leap year";
    }
    else
        cout << year << " is a leap year.";
}
else
   cout << "There is " << ???year << " years till next leap year";

return 0;

}


Solution

  • I have modified your code. Please use below code for the same and try to understand your mistakes:-

    int main()
    {
    
        int year;
    
        cout << "Enter a year: ";
        cin >> year;
        int isleap = year % 4;
        if (isleap == 0)
        {
            isleap = year % 100;
            if (isleap == 0)
            {
                isleap = year % 400;
                if (isleap == 0)
                    cout << year << " is a leap year."<<endl;
                else
                    cout << "There is " << (4-isleap) << " years till next leap year"<<endl;
            }
            else
                cout << year << " is a leap year."<<endl;
        }
        else
           cout << "There is " << (4-isleap) << " years till next leap year"<<endl;
    
        return 0;
    
    }