Home > Back-end >  A leap year determining C program is not giving any output when inputting 2000, 1900 or 2100 as an i
A leap year determining C program is not giving any output when inputting 2000, 1900 or 2100 as an i

Time:05-09

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(n0 != 0)
        {
            printf("Leap year"); 
        }
    }
    else if(n0 == 0)
    {
        if (n@0==0)
        {
            //`enter code here`
            printf("Leap year");
        }
    }
    else 
        printf("Not a Leap year");

    return 0;
}

CodePudding user response:

There is a problem in this part of the code:

if (n%4==0)
{
    if(n0 != 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 19000 is zero, and as you can see, it is zero, so it doesn't enter the if(n0!=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(n0 == 0) part because any number that is divisible by 100 is also divisible by 4.

CodePudding user response:

This if statement

    if (n%4==0)
{
    if(n0 != 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");
}

CodePudding user response:

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" );
  • Related