Home > Backend Development > C++ > body text

Write a program in C language to check if a given year is a leap year or not

王林
Release: 2023-09-20 15:33:10
forward
1484 people have browsed it

Write a program in C language to check if a given year is a leap year or not

A leap year has 366 days, while an ordinary year has 365 days. The task is to check whether a given year is a leap year through a program.

The logic of judgment can be implemented by checking whether the year is divisible by 400 or 4, but if it is not divisible by these two numbers, it is an ordinary year.

Example

Input-: year=2000
Output-: 2000 is a Leap Year

Input-: year=101
Output-: 101 is not a Leap year
Copy after login

Algorithm

Start
Step 1 -> declare function bool to check if year if a leap year or not
bool check(int year)
   IF year % 400 = 0 || year%4 = 0
      return true
   End
   Else
      return false
   End
Step 2 -> In main()
   Declare variable as int year = 2000
   Set check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year)
   Set year = 10
   Set check(year)? printf("%d is a Leap Year",year): printf("</p><p>%d is not a Leap Year",year);
Stop
Copy after login

Example

#include <stdio.h>
#include <stdbool.h>
//bool to check if year if a leap year or not
bool check(int year){
   // If a year is multiple of 400 or multiple of 4 then it is a leap year
   if (year % 400 == 0 || year%4 == 0)
      return true;
   else
      return false;
}
int main(){
   int year = 2000;
   check(year)? printf("%d is a Leap Year",year): printf("%d is not a Leap Year",year);
   year = 101;
   check(year)? printf("%d is a Leap Year",year): printf("</p><p>%d is not a Leap Year",year);
   return 0;
}
Copy after login

Output

2000 is a Leap Year
101 is not a Leap Year
Copy after login

The above is the detailed content of Write a program in C language to check if a given year is a leap year or not. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template