1.11M Members

Leap year program In C

 
0
 

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

 
0
 

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

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article