Also view this program using if else -
C program to check leap year using if else.
Required knowledge:
Basic C programming, Conditional operator, Leap year conditionLeap year condition:
If the year is EXACTLY DIVISIBLE by 4 and NOT DIVISIBLE by 100 then its LEAP YEARElse if the year is EXACTLY DIVISIBLE 400 then its LEAP YEAR
Else its a COMMON YEAR
Program:
/** * C program to check leap year using conditional operator */ #include <stdio.h> int main() { int year; /* * Reads year from user */ printf("Enter any year: "); scanf("%d", &year); (year%4==0 && year%100!=0) ? printf("LEAP YEAR") : (year%400 ==0 ) ? printf("LEAP YEAR") : printf("COMMON YEAR"); return 0; }
Note: We can also write the same program using conditional operator as:
/** * C program to check leap year using conditional operator */ #include <stdio.h> int main() { int year; /* * Reads year from user */ printf("Enter any year: "); scanf("%d", &year); printf("%s", ((year%4==0 && year%100!=0) ? "LEAP YEAR" : (year%400 ==0 ) ? "LEAP YEAR" : "COMMON YEAR")); return 0; }
Output
Enter any year: 2016
LEAP YEAR
LEAP YEAR
Happy coding ;)
You may also like
- C program to find maximum between two numbers using conditional operator.
- C program to find maximum between three numbers using conditional operator.
- C program to check even or odd number using conditional operator.
- C program to check whether a character is alphabet or not using conditional operator.
- C program to enter any two number and perform all arithmetic operations.
- C program to find total, average and percentage of marks of five subjects.
- C program to calculate Simple Interest.
- C program to check whether a triangle is Equilateral, Isosceles or Scalene triangle.
- C program to print total number of days in a month.
- C program to print day of week name using if else.
- C program to print all natural numbers from 1 to n.
- C program to print all alphabets from a to z.
- C program to find power of any number using for loop.