1,105,534 Community Members

Leap year program In C

Member Avatar
seo2005
Light Poster
28 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
SecurExpert
Newbie Poster
24 posts since Sep 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article