Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

6. write a program to check leap year or not in c [easy code].

C
O
D
E

PROGRAM TO CHECK LEAP YEAR:

#include<stdio.h>

int main()
{
int year;

printf(“Enter a year: “);
scanf(“%d”, &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf(“%d is a leap year.\n”, year);
}
else
{
printf(“%d is not a leap year.\n”, year);
}

return 0;
}

OUTPUT:

Enter a year: 2004
2004 is a leap year.

DOWNLOAD CODE:

LEAP YEAR IN C PROGRAM PDF DOWNLOAD

Table of Contents

P R O G R A M M I N G

C PROGRAMPROGRAM TO CHECK LEAP YEAR OR NOT IN C PROGRAM

PROGRAM TO CHECK LEAP YEAR

E X E C U T E

TO CHECK GIVEN YEAR IS LEAP YAER OR NOT IN C PROGRAM COMPILE
C PROGRAM TO CHECK LEAP YEAR

E X P L A N A T I O N

EXPLAIN PROGRAM TO CHECK LEAP YEAR:

ThIs C program checks if a given year is a leap year and prints the result.

Program Overview:

This C program to check leap year serves a straightforward purpose. A leap year has an extra day (February 29) and occurs every four years, with some exceptions. The code utilizes if and else statements to evaluate the conditions that define a leap year and then provides output accordingly.

Code Explanation:

  1. #include <stdio.h>: This line is a preprocessor directive that includes the standard input-output library in the program. It’s necessary for input and output operations.
  2. int main(): This is the main function of the program. It is the entry point of the program, and execution starts from here.
  3. int year;: This line declares an integer variable named year to store the user’s input, which will represent the year to be checked.
  4. printf("Enter a year: ");: This statement uses the printf function to display the “Enter a year: ” message on the console, prompting the user to input a year.
  5. scanf("%d", &year);: This line uses the scanf function to read the integer value entered by the user and store it in the year variable. The %d format specifier is used to read an integer.
  6. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {: This is the beginning of the if statement. It checks the conditions for a leap year.
    • (year % 4 == 0 && year % 100 != 0): The first part of the condition checks whether the year is divisible by 4 (i.e., year % 4 == 0) and not divisible by 100 (i.e., year % 100 != 0). This is a common rule for leap years.
    • ||: This is a logical OR operator, which means the condition will be true if either the first part or the second part is true.
    • (year % 400 == 0): The second part of the condition checks whether the year is divisible by 400. This is an exception to the first condition. If a year is divisible by 400, it is considered a leap year.
  7. printf("%d is a leap year.\n", year);: If the condition in the if statement is true, this line will execute. It uses the printf function to print a message indicating that the input year is a leap year, along with the year itself.
  8. else {: If the condition in the if statement is false, execution proceeds to the else block.
  9. printf("%d is not a leap year.\n", year);: In the else block, this line uses printf to display a message indicating that the input year is not a leap year, along with the year itself.
  10. return 0;: Finally, the return statement is used to exit the program. The value 0 indicates a successful execution of the program. The main function is of type int, and it’s customary to return an integer value to indicate the program’s status (0 for success).

How the Code Works:

  1. The program starts by prompting the user to input a year.
  2. The user enters a year, and the program stores it in the year variable.
  3. The if statement is used to check whether the input year is a leap year, based on the conditions defined for leap years. If the conditions are met, the program prints that it’s a leap year. Otherwise, it prints that it’s not a leap year.
  4. The program exits with a status of 0, indicating successful execution.

Leap Year Calculation:

The code is based on the rules for determining leap years:

  • A year is a leap year if it’s divisible by 4.
  • However, years divisible by 100 are not leap years, unless…
  • …they are divisible by 400, in which case they are considered leap years.

For example, the year 2000 is a leap year because it’s divisible by 4 and also divisible by 400. The year 1900, on the other hand, is not a leap year because it’s divisible by 4 and 100, but not by 400.

Sample Execution:

Let’s illustrate how the code works with a sample execution:

  1. The program prompts the user to enter a year.
  2. The user enters the year 2024.
  3. The program checks the conditions: (2024 % 4 == 0 && 2024 % 100 != 0) is true, and (2024 % 400 == 0) is not needed. Therefore, the program prints “2024 is a leap year.”
  4. The program exits with a status of 0.

Conclusion:

In summary, this C program checks if a given year is a leap year by applying the rules that define leap years. It uses the if and else statements to evaluate the conditions and provides a clear message to the user based on the outcome of the check. The program is a simple but useful tool for identifying leap years, and it demonstrates the use of conditional statements in C.

MORE PROGRAM :

BEST C PROGRAMMING BOOK :

Leave a Comment