C program to find a leap year

Leap year comes after every 4 years. A number is said to be a leap year if the number is divisible by 4 but not by 100 then, it is a leap year. Also, if a number is divisible by 4, 100 and 400 then it is a leap year. If the above conditions does not satisfy then the year is not a leap year.
Example: 2008, 2012 etc.,
We can write the program in different ways, few of them are given below:

Program to accept a year and check whether the given year is a leap year or not. 

Program:

/*C program to finf leap year using if statement */

# include <stdio.h>
# include <conio.h>
main( ) 
{
 int y; 
clrscr( ); 
printf(“Enter a year:”);
 scanf(“%d”, &y); 
if(y%4==0& &y%100!=0|| y%400==0); 
  printf(“\nThe given year is a Leap year”); 
else 
   printf(“\nThe given year is Not a Leap year”); 
getch(); 
}
Output:
Enter a year: 2000

The given year is a Leap year


Program to accept a year and check the given year is leap or not by using ternary operator

Program:
/*C program of leap year */

 
# include<stdio.h>

# include<conio.h>
main( ) 
{
 int yr,leap; 
clrscr( );
 printf(''Enter a year: "); 
scanf("%d", &yr); 
leap=(yr % 400==0)?:(yr %100 != 0)?(yr %4 ==0)?1:0:0; 
if(leap= =1)
   printf(“\nThe given year is Leap year”);
else 
   printf(“\nThe given year is Not Leap year); 
getch( ); 
}

Output:
Enter a year: 2000

The given year is a Leap year


No comments:

Post a Comment