11.10.2012

User Define Function- LeapYear

Q. Write a C program to find entered year is leap year or not using own created function say "leap".

Ans.

/*c program for find year is leap year or not using user define function leap*/
#include<stdio.h>
int leap(int );
int main()
{

 int year;
 printf("Enter any year : ");
 scanf("%d", &year);
 if(leap(year))
   printf("\n%d is leap year",year);
 else
   printf("\n%d is not leap year",year);
 return 0;
}

int leap(int y)
{
 if((y%400==0 && y%100==0)||(y%4==0))
    return 1;
 else
    return 0;
}

The output of above program would be:


Output of check year is leap year or not using user define function C program
Figure: Screenshot for user define function to
check year is leap year C program



Output of check year is leap year or not using user define function C program
Figure: Screenshot for user define function to 
check year is not leap year C program


2 comments:

  1. if(leap(year))......what is mean this line

    ReplyDelete
  2. The Above code is WRONG CODE
    THE below one is the right one
    All years which are perfectly divisible by 4 are leap years except for century years( years ending with 00 ) which is a leap year only it is perfectly divisible by 400. For example: 2012, 2004, 1968 etc are leap year but, 1971, 2006 etc are not leap year. Similarly, 1200, 1600, 2000, 2400 are leap years but, 1700, 1800, 1900 etc are not.

    This program asks user to enter a year and this program checks whether that year is leap year or not.
    /* C program to check whether a year is leap year or not using if else statement.*/

    #include
    int main(){
    int year;
    printf("Enter a year: ");
    scanf("%d",&year);
    if(year%4 == 0)
    {
    if( year%100 == 0) /* Checking for a century year */
    {
    if ( year%400 == 0)
    printf("%d is a leap year.", year);
    else
    printf("%d is not a leap year.", year);
    }
    else
    printf("%d is a leap year.", year );
    }
    else
    printf("%d is not a leap year.", year);
    return 0;
    }

    ReplyDelete