leap year or not
Table of Contents
Algorithm
- Start
- Read the input year from the user and store it in a variable, let’s call it
year. - Check if
yearis divisible by 4 and not divisible by 100, or if it is divisible by 400.- If the condition is true, the year is a leap year.
- If the condition is false, the year is not a leap year.
- Display the result indicating whether the year is a leap year or not.
- End
By following this algorithm, you can implement a program in any programming language to determine whether a given year is a leap year or not.
C Program
#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;
}
C++
#include <iostream>
int main() {
int year;
std::cout << "Enter a year: ";
std::cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
std::cout << year << " is a leap year." << std::endl;
} else {
std::cout << year << " is not a leap year." << std::endl;
}
return 0;
}
Both the C and C++ programs follow the same logic. They prompt the user to enter a year, read it using scanf in C and std::cin in C++, and store it in the variable year.
Then, they use an if statement to check if the year satisfies the conditions for a leap year. According to the rules, a leap year must satisfy one of the following conditions:
- It is divisible by 4 but not divisible by 100.
- It is divisible by 400.
If the year satisfies any of these conditions, it is considered a leap year. Otherwise, it is not a leap year.
Finally, they display the result using printf in C and std::cout in C++.
Python
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
In this program, the user is prompted to enter a year using the input() function, and the year is stored in the variable year after converting it to an integer using the int() function.
Then, an if statement is used to check if the year satisfies the conditions for a leap year. According to the rules, a leap year must satisfy one of the following conditions:
- It is divisible by 4 but not divisible by 100.
- It is divisible by 400.
If the year satisfies any of these conditions, it is considered a leap year. Otherwise, it is not a leap year.
Finally, the program uses the print() function to display the result indicating whether the year is a leap year or not.
Java
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
In this Java program, we use the Scanner class to read the input from the user. The program prompts the user to enter a year and stores it in the variable year.
Then, we use an if statement to check if the year satisfies the conditions for a leap year. According to the rules, a leap year must satisfy one of the following conditions:
- It is divisible by 4 but not divisible by 100.
- It is divisible by 400.
If the year satisfies any of these conditions, it is considered a leap year. Otherwise, it is not a leap year.
Finally, we display the result using System.out.println().
Remember to import the java.util.Scanner class at the beginning of your program to use the Scanner class for input reading.
I was suggested this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!
Really instructive and fantastic complex body part of articles, now that’s user friendly (:.
I went over this site and I think you have a lot of wonderful information, saved to bookmarks (:.