C Exercises: Check whether a given year is a leap year or not
C Conditional Statement: Exercise-4 with Solution
Write a C program to find whether a given year is a leap year or not.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int chk_year; // Declare an integer variable 'chk_year'.
printf("Input a year :"); // Prompt the user to input a year.
scanf("%d", &chk_year); // Read and store the user's input in 'chk_year'.
if ((chk_year % 400) == 0) // Check if 'chk_year' is divisible by 400 with no remainder.
printf("%d is a leap year.\n", chk_year); // Print a message indicating that 'chk_year' is a leap year.
else if ((chk_year % 100) == 0) // Check if 'chk_year' is divisible by 100 with no remainder.
printf("%d is not a leap year.\n", chk_year); // Print a message indicating that 'chk_year' is not a leap year.
else if ((chk_year % 4) == 0) // Check if 'chk_year' is divisible by 4 with no remainder.
printf("%d is a leap year.\n", chk_year); // Print a message indicating that 'chk_year' is a leap year.
else
printf("%d is not a leap year \n", chk_year); // Print a message indicating that 'chk_year' is not a leap year.
}
Sample Output:
Input a year :2016 2016 is a leap year.
Flowchart:
C Programming Code Editor:
Previous: Write a C program to check whether a given number is positive or negative.
Next: Write a C program to read the age of a candiadte and detrermine whether it is eligible for casting his/her own vote.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming-exercises/conditional-statement/c-conditional-statement-exercises-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics