输入年份,如果不是闰年,不使用模数符号,则显示“闰年"if it is leap year or "不是闰年”的程序

A program that inputs a year and displays “Leap year "if it is leap year or "Not leap year" if it is not leap year without using the modulus symbol

到目前为止我已经想到了这个但是它似乎并没有始终如一地输出结果。

if (year/4*4==year)
{
cout<<"It is a leap year!";
}
else
{
cout<<"It is not a leap year!";
}

我该如何解决这个问题?

如果要判断用户输入的年份是不是闰年,只需要写一个循环即可。

在我的代码中,我还添加了一个辅助函数(Turbo C++ 有 bool 类型,对吧?它是标准的一部分...),具有准确的公式(您可以在网上找到)。请注意,当用户输入的内容不是整数(或 EOF)时,循环结束。

#include <iostream>

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

int main()
{
    int year;

    while ( std::cin >> year )
    {
        if ( is_leap(year) )
        {
            std::cout << year << " is a leap year!\n";
        }
        else
        {
            std::cout << year << " is not a leap year.\n";
        }
    }

    return 0;
}

比如你可以试试:

1857 is not a leap year.
1900 is not a leap year.
1980 is a leap year!
1991 is not a leap year.
2000 is a leap year!
2016 is a leap year!