确定C计划的leap年份在输入2000、1900或2100作为输入时没有给出任何输出

发布于 2025-01-27 12:28:58 字数 701 浏览 3 评论 0原文

当我运行此C程序,并给2020年,2024年或其他几年以4分的序幕,然后我获得了预期的输出,即这是leap年。 但是,当我给一个世纪(1900、2000或2100等)时,作为输入,它没有给我任何输出。

请不要建议我正确的程序,因为有很多 在互联网上可用,我已经理解了它们。请您 告诉我为什么我的程序在输入时不给出任何输出 世纪。

ps此程序没有错误。

程序:

#include <stdio.h>

int main()
{
    int n;
    printf("Enter year : "); scanf("%d",&n);
    
    if (n%4==0)
    {
        if(n%100 != 0)
        {
            printf("Leap year"); 
        }
    }
    else if(n%100 == 0)
    {
        if (n%400==0)
        {
            //`enter code here`
            printf("Leap year");
        }
    }
    else 
        printf("Not a Leap year");

    return 0;
}

When I am running this C program, and giving 2020, 2024 or other years perfectly divisible by 4, then I am getting expected output i.e. it is a leap year.
But when I am giving a century year : 1900, 2000 or 2100 etc. as an input, then it's not not giving me any output.

Please do not suggest me the correct program as there are many
available on internet and I understood them already. You are requested
to tell me why my program is not giving any output when entering a
century year.

P.S. this program gives no error.

Program:

#include <stdio.h>

int main()
{
    int n;
    printf("Enter year : "); scanf("%d",&n);
    
    if (n%4==0)
    {
        if(n%100 != 0)
        {
            printf("Leap year"); 
        }
    }
    else if(n%100 == 0)
    {
        if (n%400==0)
        {
            //`enter code here`
            printf("Leap year");
        }
    }
    else 
        printf("Not a Leap year");

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ低調成傷 2025-02-03 12:28:58

代码的这一部分存在一个问题:

if (n%4==0)
{
    if(n%100 != 0)
    {
        printf("Leap year"); 
    }
}

当您进入1900时,它会进入第一个条件。 (因为1900%4等于零。)然后,它检查了1900%100是否为零,如您所见,它是零,因此它不会输入IF(n%100!= 0) )条件,在if(n%4 == 0)中没有其他语句。因此,没有代码输入的条件。因此,它没有提供任何输出。
加上您的代码永远不会输入如果(n%100 == 0)零件,因为100可以除以100的任何数字也可以除以4。

There is a problem in this part of the code:

if (n%4==0)
{
    if(n%100 != 0)
    {
        printf("Leap year"); 
    }
}

When you enter 1900, it enters the first condition. (Because 1900%4 is equal to zero.) And then, it checks whether or not 1900%100 is zero, and as you can see, it is zero, so it doesn't enter the if(n%100!=0) condition, and there is no other else statement in the if (n%4==0). So there is no condition for the code to enter. And therefore it doesn't give any output.
Plus your code never enters else if(n%100 == 0) part because any number that is divisible by 100 is also divisible by 4.

御弟哥哥 2025-02-03 12:28:58

如果声明

    if (n%4==0)
{
    if(n%100 != 0)
    {
        printf("Leap year"); 
    }
}

获得了1900年之类的声明,因为它们可以被4个排除。但是,这样一年也可以被100个排除,那么什么也没有输出。

因此您的程序有逻辑错误。

您可以重写if语句,例如以下方式

if ( ( n % 4 != 0 ) || ( n % 100 == 0 && n % 400 != 0 ) )
{
    printf("Not a Leap year");
}
else
{ 
    printf("Leap year"); 
}

if ( ( n % 4 == 0 ) && ( n % 400 == 0 || n % 100 != 0 ) )
{
    printf("Leap year"); 
}
else
{ 
    printf("Not a Leap year");
}

或使用嵌套if-else语句

if ( n % 4 == 0 )
{
    if ( n % 400 == 0 )
    {
        printf("Leap year"); 
    }
    else if ( n % 100 != 0 )
    {
        printf("Leap year"); 
    }
    else
    {
        printf("Not a Leap year");
    }
}
else
{ 
    printf("Not a Leap year");
}

This if statement

    if (n%4==0)
{
    if(n%100 != 0)
    {
        printf("Leap year"); 
    }
}

gets the control for years like 1900 because they are divisible by 4. But as such a year is also divisible by 100 then nothing is outputted.

So your program has a logical error.

You could rewrite the if statements for example the following way

if ( ( n % 4 != 0 ) || ( n % 100 == 0 && n % 400 != 0 ) )
{
    printf("Not a Leap year");
}
else
{ 
    printf("Leap year"); 
}

or

if ( ( n % 4 == 0 ) && ( n % 400 == 0 || n % 100 != 0 ) )
{
    printf("Leap year"); 
}
else
{ 
    printf("Not a Leap year");
}

Or if to use nested if-else statements then the code can look like

if ( n % 4 == 0 )
{
    if ( n % 400 == 0 )
    {
        printf("Leap year"); 
    }
    else if ( n % 100 != 0 )
    {
        printf("Leap year"); 
    }
    else
    {
        printf("Not a Leap year");
    }
}
else
{ 
    printf("Not a Leap year");
}
刘备忘录 2025-02-03 12:28:58

您的逻辑具有以下结构:

if ( n % 4 == 0 )
{
    if ( n % 100 != 0 )
    {
        printf( "Leap year\n" ); 
    }
}

else if ( n % 100 == 0 )
{
    if ( n % 400 == 0 )
    {
        printf( "Leap year\n" );
    }
}

else
{
    printf( "Not a Leap year\n" );
}

如果,我将添加Braces { }而不是编写else else else else 语句,以清楚说明else实际上是指的内容。添加牙套不会改变程序的行为。

if ( n % 4 == 0 )
{
    if ( n % 100 != 0 )
    {
        printf( "Leap year\n" ); 
    }
}
else
{
    if ( n % 100 == 0 )
    {
        if ( n % 400 == 0 )
        {
            printf( "Leap year\n" );
        }
    }
    else
    {
        printf( "Not a Leap year\n" );
    }
}

现在,您可以清楚地看到,如果如果条件n%4 == 0是正确的,则将执行第一个外部块,否则第二个外部块将执行。

但是,这种逻辑是错误的。如果条件n%4 == 0是正确的,则将执行第一个外部块,这意味着您的程序将打印“ leap Year”如果>如果条件n%100!= 0是正确的,但是如果条件为false,则无需执行任何操作(即什么都不打印)。这不是您想要的。

您想要的是以下逻辑:

if ( n % 4 != 0 )
{
    printf( "Not a Leap year\n" );
}
else
{
    if ( n % 100 != 0 )
    {
        printf( "Leap year\n" );
    }
    else
    {
        if ( n % 400 != 0 )
        {
            printf( "Not a Leap year\n" );
        }
        else
        {
            printf( "Leap year\n" );
        }
    }
}

这可以通过卸下所有牙套来更紧凑:

if ( n % 4 != 0 )
    printf( "Not a Leap year\n" );

else if ( n % 100 != 0 )
    printf( "Leap year\n" );

else if ( n % 400 != 0 )
    printf( "Not a Leap year\n" );

else
    printf( "Leap year\n" );

Your logic has the following structure:

if ( n % 4 == 0 )
{
    if ( n % 100 != 0 )
    {
        printf( "Leap year\n" ); 
    }
}

else if ( n % 100 == 0 )
{
    if ( n % 400 == 0 )
    {
        printf( "Leap year\n" );
    }
}

else
{
    printf( "Not a Leap year\n" );
}

Instead of writing else if, I will add braces { } to every else statement, to make it clear what the else is actually referring to. Adding the braces does not change the behavior of your program.

if ( n % 4 == 0 )
{
    if ( n % 100 != 0 )
    {
        printf( "Leap year\n" ); 
    }
}
else
{
    if ( n % 100 == 0 )
    {
        if ( n % 400 == 0 )
        {
            printf( "Leap year\n" );
        }
    }
    else
    {
        printf( "Not a Leap year\n" );
    }
}

Now you can clearly see that if the if condition n % 4 == 0 is true, then the first outer block will be executed, otherwise the second outer block will be executed.

However, this logic is wrong. If the condition n % 4 == 0 is true, then the first outer block will be executed, which means that your program will print "Leap year" if the if condition n % 100 != 0 is true, but will do nothing (i.e. print nothing) if that if condition is false. This is not what you want.

What you want is rather the following logic:

if ( n % 4 != 0 )
{
    printf( "Not a Leap year\n" );
}
else
{
    if ( n % 100 != 0 )
    {
        printf( "Leap year\n" );
    }
    else
    {
        if ( n % 400 != 0 )
        {
            printf( "Not a Leap year\n" );
        }
        else
        {
            printf( "Leap year\n" );
        }
    }
}

This can be more compactly written by removing all braces:

if ( n % 4 != 0 )
    printf( "Not a Leap year\n" );

else if ( n % 100 != 0 )
    printf( "Leap year\n" );

else if ( n % 400 != 0 )
    printf( "Not a Leap year\n" );

else
    printf( "Leap year\n" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文