Skip to main content

calculating leap year in c using if else

Most new students get really confused when it comes to calculating Leap year in C. It is given as an exercise to enhance their knowledge about using if else usages. This can be calculated using Logical operators too. But using if else provides clearer ways to calculate it. Both of the methods can be implemented in C/C++ program. Here we will discuss the if else method in C/C++


Leap year : A year is Leap year if
  1. It is not a century and gets divided by 4.
  2. It is a century and gets divided by 400.
it is clear that a year will either be century (like 1600,1700,1800,1900) or it will not be a century (like 1988,1999 etc). You get the picture right.
So the first step is to check if the given year is a century or not? it can be easily checked using modulus operator % . (Modulus operator calculates remainder) . So if the modulus gives us 0, that means the given number is divided exactly.
So 1700,1800,1900 are not Leap Years (as they all are centuries but not divided by 400).
Years like 1600,2000,2400 are Leap Years (they all are divided exactly vy 400).
On the other hand Years like 1946,1950,1950 are not Leap year (they are not centuries and are not divided exactly by 4).
While years like 1944,1948,1952 are Leap years (as they are not centuries and are divided by 4 exactly ).
How can we calculate leap year in c/ c++? well its easy:


Program:


#include<stdio.h>
int leapyear(int);// protyping function

int main()
{
    int x;
    printf("Enter a number=");
    scanf("%d",&x);
    if(leapyear(x))
    {
        printf("The %d year is Leap year",x);
    }
    else
    {
        printf("The %d year is not Leap year",x);
    }
}
int leapyear(int year)
{
    int iscentury=0;// to test if year is century or not
    if(year%100==0)
    {
        iscentury=1;//yes its a century
    }
    if(iscentury)
    {
        if(year%400==0)//century must be divided by 400
        {
            //yes?
            return(1);// you have leap year... return 1
        }
        else
        {
            return(0);// no leap year return 0
        }
    }
    else
    {
        if(year%4==0)// normal year must be divided by 4
        {
            return(1);// you have leap year return 1;    
        }
        else
        {
            return(0);// you don't have leap year return 0
        }       
    }  
}
The above C program returns 1 if it finds a Leap year otherwise returns 0 if it doesn't. easy right?

Popular posts from this blog

Overloading Unary Operator in C++

In this Example I will demonstrate overloading a unary operator. The overloading is done in similar way as we did for binary operator. In this example I will be using a Vector Class. The operator we are going to over load is the unary "~" (tilde) operator. although you can use any unary overloadable operator. Method: In this example the overloaded operator will be the part of the class i.e. the over loaded definition will be a member function. (As we know overloading can be done using non member functions too). So the unary operator will not have any arguments in it. We will overload ~ operator so that it will reflect the vector so that its z component becomes x component and vice versa. So input vector will be 2i+3j+5k and its output vector will be 5i+3j+2k. although you can do anything you like. Code: # include < iostream > using namespace std ; class vector { int x , y , z ;      public : vector ( ) { x = y = z = 0 ; ...

Program to find nth Prime number in C

Objective: To print any Prime number in C.  This task may sound easy but is a very good exercise to help optimize CPU intensive programs. Finding Prime number is a very CPU hogging task and it becomes even more dreaded as the number starts to grow. Lets say we want to find 30001th prime. how will we find it?? Here is the program. Programming logic : The easiest method is to check if the given number N is divisible by any number from 2 to N-1. But it may be noted that we don't need to go beyond the number that is square root of N.  why?? lets have a look. Say we are testing number 49 for prime number. Our objective is to find any number that may divide it. so we start from 2,3,4 etc. As soon as we cross 7 there will be no number that will divide 49, if there was we have got it earlier since it will be smaller then 7.  Get it? another example 64 : 2x32 4x16 8x8 16x4 32x2 as you can see when we pass number 8 we don't need to test as the number that will come ,...