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

Find nth Prime Number in C++

c++ program to find prime numbers: The problem of finding prime number can be solved by checking all numbers, testing them for prime and then moving ahead. If you want to calculate nth prime. Then this can be done in a brutal way by checking the number one by one. This may sound odd, by there is no easy way then this  for prime numbers (Well Actually there are like Pollard's Rho Algorithm, Number Sieves or Shor's Quantum Algorithm, but we are talking about the one that most people may understand easily). There may be way to pre-calculate the prime numbers but that again is not sufficient. So how can we use c++ to create a program to find prime numbers.

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 ; ...