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


4 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
  3. Somem mistake in the coding of this program i.e
    In this expression
    if((y%400==0 && y%100==0)||(y%4==0))
    It is wrong
    Right expression is that
    if((y%400==0|| y%4==0)&&(y%100!=0))
    1900,2100,1700,2300 it is not a leap year but your program tell that it is a leap year.
    Now you use my expression in your program instead of your. Then it tells actual result.
    Thank you

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete