BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to check leap year using ternary operator

Last Updated: June 1, 2024 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to check leap year using ternary operator. This program prompts user to enter a year. It then checks if it’s a leap year or not using the ternary operator. Let’s break down the condition used in the following program:

  • Entered year is divisible by 4
  • Not divisible by 100 unless it’s also divisible by 400.

If both the above statements return true then the condition as whole return true and "Leap year" is assigned to result String else "Not a leap year" is assigned.

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();

// Using ternary operator to check if the year is a leap year
String result = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? "Leap year" : "Not a leap year";

System.out.println(year + " is " + result);

scanner.close();
}
}

Output:

Enter a year: 2024
2024 is Leap year

In this example, the user entered 2024, the program output it as a leap year because it’s divisible by 4 and not divisible by 100 unless it’s also divisible by 400.

Top Related Articles:

  1. Java Program to find ASCII value of a Character
  2. Java Program to Calculate average using Array
  3. Java Program to display first n or first 100 prime numbers
  4. Java Program to Perform Arithmetic operation using Method Overloading
  5. Java Program to find largest of three numbers using Ternary Operator

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap