Write a C program to check whether a entered year is a leap year or not. In this tutorial, we are going to write a code which take a year as an input and print whether a input year is a leap year or not.
A Leap year is a year which has 366 days, it means it has one additional day. Each leap year has 366 days instead of the usual 365 days.
i) If a year is divisible by 4 and not divisible by 100 then it's a leap year. For example - 2012, 2008 etc.
ii) If a century year (1200, 1600) is divisible by 400 then it's a leap year. For example - 1200, 1600 etc.
C, C++ Interview Questions
C program to print factorial of a number
C program to calculate power of a number
We have discussed what is leap and algorithm to check leap year. Let's write a code which takes an input (year) and check whether a input year is a leap year or not.
Another way of writing above program.
C program to check whether a number is Palindrome or not
In this programming example, let's create a function which checks whether an input year is leap year or not.
Output:
Enter a year : 2008
2008 is a leap year
What is Leap Year?
A Leap year is a year which has 366 days, it means it has one additional day. Each leap year has 366 days instead of the usual 365 days.
Algorithm to Check Leap Year
i) If a year is divisible by 4 and not divisible by 100 then it's a leap year. For example - 2012, 2008 etc.
ii) If a century year (1200, 1600) is divisible by 400 then it's a leap year. For example - 1200, 1600 etc.
C, C++ Interview Questions
C program to print factorial of a number
C program to calculate power of a number
C Program to Check Leap Year using Conditional Operator
We have discussed what is leap and algorithm to check leap year. Let's write a code which takes an input (year) and check whether a input year is a leap year or not.
#include <stdio.h> int main(void) { int year; printf("Enter a year\n"); scanf("%d",&year); /* Leap is divisible by 4 but not by 100 or it is divisible by 400 */ if( ( (year%4 == 0) && (year%100!=0) ) || (year%400==0) ) { printf("%d is a leap year.", year ); } else { printf("%d is not a leap year.", year); } return 0; }
Another way of writing above program.
C program to check whether a number is Palindrome or not
#include <stdio.h> int main(void) { int year; printf("Enter a year \n"); scanf("%d",&year); /* Check whether a entered year is divisible by 4 */ if(year%4 == 0) { /* For century year such as 500,1200 etc */ if( year%100 == 0) { /* If it's divisible by 400 then it's a leap 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; }
C Program to Check Leap Year using Function
In this programming example, let's create a function which checks whether an input year is leap year or not.
#include <stdio.h> bool checkLeapYear(int year) { /* Leap is divisible by 4 but not by 100 or it is divisible by 400 */ if( ( (year%4 == 0) && (year%100!=0) ) || (year%400==0) ) { return true; } else { return false; } } int main(void) { int year; printf("Enter a year\n"); scanf("%d", &year); if( checkLeapYear(year) ) { printf(" %d is a leap year.", year ); } else { printf("%d is not a leap year.", year); } return 0; }
Output:
Enter a year : 2008
2008 is a leap year
No comments:
Post a Comment