Hi,
I don't understand the if statement here. Can anyone explain how the execution of if loop will proceed.

Why are we using || and && operators here.

#include<stdio.h>

void main
{
    int year;

    printf("Enter the year: ");
    scanf("%d",&year);

     if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
    {
        printf("Year %d is a leap year",year);
    }
    else
    {
        printf("Year %d is not a leap year",year);
    }

Thanks

Hi,
I don't understand the if statement here. Can anyone explain how the execution of if loop will proceed.

Why are we using || and && operators here.

#include<stdio.h>

void main
{
int year;

printf("Enter the year: ");
scanf("%d",&year);

if(year%400 ==0 || (year%100 != 0 && year%4 == 0))
{
printf("Year %d is a leap year",year);
}
else
{
printf("Year %d is not a leap year",year);
}

Well, you see, according this program, a leap year occurs when the year is divisible by 400 with no remainder as in

if(year%400==0..

OR (mark this) when the year is divisible by 100 and (mark this also) also divisible by 4 and with no reminder in both as in

.. || (year%100 != 0 && year%4 != 0)

The

||

is the logical OR in C.
So if the above conditions are not met, it is not a leap year and therefore the else condition is executed.
Hope that helps you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.