确定C计划的leap年份在输入2000、1900或2100作为输入时没有给出任何输出
当我运行此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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码的这一部分存在一个问题:
当您进入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:
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.如果声明
获得了1900年之类的声明,因为它们可以被4个排除。但是,这样一年也可以被100个排除,那么什么也没有输出。
因此您的程序有逻辑错误。
您可以重写if语句,例如以下方式
或
或使用嵌套if-else语句
This if statement
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
or
Or if to use nested if-else statements then the code can look like
您的逻辑具有以下结构:
如果,我将添加Braces {
}
而不是编写else
else else else 语句,以清楚说明else
实际上是指的内容。添加牙套不会改变程序的行为。现在,您可以清楚地看到,如果
如果
条件n%4 == 0
是正确的,则将执行第一个外部块,否则第二个外部块将执行。但是,这种逻辑是错误的。如果条件
n%4 == 0
是正确的,则将执行第一个外部块,这意味着您的程序将打印“ leap Year”
如果>如果
条件n%100!= 0
是正确的,但是如果条件为false,则无需执行任何操作(即什么都不打印)。这不是您想要的。您想要的是以下逻辑:
这可以通过卸下所有牙套来更紧凑:
Your logic has the following structure:
Instead of writing
else if
, I will add braces{
}
to everyelse
statement, to make it clear what theelse
is actually referring to. Adding the braces does not change the behavior of your program.Now you can clearly see that if the
if
conditionn % 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 theif
conditionn % 100 != 0
is true, but will do nothing (i.e. print nothing) if thatif
condition is false. This is not what you want.What you want is rather the following logic:
This can be more compactly written by removing all braces: