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.

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