C Program to accept a year and check the given year is leap or not by using ternary

CWC
8 Min Read

C Program to accept a year and check the given year is leap or not by using ternary.

This program is a simple program that takes an input of number of years as a parameter and print “It’s leap year” if the parameter is even or else it will print “it’s not leap year”.

So here, let us see how to check if a year is leap or not using ternary. We can easily implement this in the C program. To do this, we first define a variable to take the value of the year. Next, we check the value of the year by comparing it with the values of leap and nonleap year, and finally print a statement to check whether it is leap year or not. Here is the code snippet to do this:-



# include <stdio.h>
# include <conio.h>
main( )
{
int y,leap;
clrscr( );
printf(“enter any yr”);
scanf(“%d”,&y);
leap=(y%400= =0)?:(y%100!=0)?(y%4=
=0)?1:0:0;
if(leap= =1)
printf(“ the given year is leap year”);
else
printf(“given year is not leap year);
getch( );
}
Updated Verion:
#include <stdio.h>
int main() {
int year;
int isLeapYear = 0; // Flag to indicate if a year is a leap year
printf("Enter any year: ");
if(scanf("%d", &year) != 1) {
printf("Invalid input. Please enter a valid year.\n");
return 1; // Exit the program if the input is not valid
}
// Leap year logic
if (year % 400 == 0) {
isLeapYear = 1;
} else if (year % 100 == 0) {
isLeapYear = 0;
} else if (year % 4 == 0) {
isLeapYear = 1;
}
// Displaying the result
if (isLeapYear == 1) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}

Expected Output

The program will prompt the user to enter a year. If the user enters a valid year (a number), the program will check if it’s a leap year and display the result. For instance, if the user enters “2024”, the output will be “2024 is a leap year.” If the input is invalid (like a character), the program will display “Invalid input. Please enter a valid year.”

Code Explanation.

  • Header Inclusion and main Function: We include `<stdio.h>` for input/output functions. The `main()` function is the entry point of the C program.
  • Variable Declaration: `year` stores the user input, and `isLeapYear` is a flag variable to indicate if a year is a leap year.
  • Input Validation: We use `scanf` to get the user input. The program checks if the input is a valid number. If not, it prints an error message and exits.
  • Leap Year Logic: The program checks if the year is divisible by 400 (leap year), not divisible by 100 (not a leap year), or divisible by 4 (leap year). The result is stored in `isLeapYear`.
  • Displaying the Result: Based on the `isLeapYear` flag, the program prints whether the input year is a leap year or not.
  • Return Statement: Finally, the program returns 0, indicating successful execution.
  • This revamped code is not only more readable but also includes input validation, making it more robust. Gone are the days of quirky, error-prone code – let’s embrace clean and efficient programming! 🚀👩‍💻

New Version: 1 Update: 04/01/2024

So, we’re gonna write a C program that’s like a ninja – quick, efficient, and super smart. Our mission? To find out if a year is a leap year or just your regular, run-of-the-mill year. And guess what? We’re gonna use the coolest trick in the book – the ternary operator. Fasten your seatbelts; it’s coding time!

 The Leap Year Lowdown

Before we code, let’s chat a bit about leap years. They’re like the special guests that pop up every 4 years, bringing an extra day in February. But it’s not just any old “every 4 years” thing. There are rules, like in a game of chess. Here they are:

  1. Any year divisible by 4 is usually a leap year.
  2. But wait, if it’s divisible by 100, it’s NOT a leap year.
  3. Hold on, there’s a twist! If it’s divisible by 400, then it’s a leap year again.

Got it? Good! Now, let’s put on our coding hats!

Coding the Leap Year Checker

We’re gonna use the ternary operator in C, which is like the Swiss Army knife of operators. It’s a neat way to do an ‘if-else’ without all the fluff. Here’s the format: condition ? expression_if_true : expression_if_false;

$h3start The Ingredients (Variables) $h3close

  • int year; – This is where we’ll store the year we want to check.

The Recipe (Code)

  1. First, we ask our user to give us a year: printf("Enter a year: ");
  2. Then, we listen (well, our program does) and store that year: scanf("%d", &year);
  3. Here comes the magic! We use our ternary operator to check if it’s a leap year:

(year % 400 == 0) ? printf("%d is a leap year.", year) :
(year % 100 == 0) ? printf("%d is not a leap year.", year) :
(year % 4 == 0) ? printf("%d is a leap year.", year) :
printf("%d is not a leap year.", year);

The Full Code

Here’s how the whole code looks like, from head to toe:


#include <stdio.h>

int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);

    (year % 400 == 0) ? printf("%d is a leap year.\n", year) :
    (year % 100 == 0) ? printf("%d is not a leap year.\n", year) :
    (year % 4 == 0) ? printf("%d is a leap year.\n", year) :
    printf("%d is not a leap year.\n", year);

    return 0;
}

A Little Reflection

So, there you have it! A snazzy C program to tell if a year is showing off its leap year vibes or just chilling as a normal year. It’s like a time-traveling detective, but in code. And remember, the ternary operator is your friend for quick decisions. It’s like choosing your outfit in the morning – fast, efficient, and gets the job done.

 

Share This Article
2 Comments
  • The code quality isn’t good. It does not compile for many reasons:
    – quotation marks are not compiler conform and need to be replaced
    – the last printf() is missing the closing quote
    – the spaces between the two equal signs cannot be compiled

    Also:
    – the code block has no indentation, no syntax highlighting. This should be standard on a website that provides code examples for developers.
    – clrscr is not everywhere supported (I don’t know why though)
    – needing conio for a simple function like getch() isn’t good style. It’s just not needed. On onlinegdb it doesn’t do what it should do: https://www.geeksforgeeks.org/getch-function-in-c-with-examples/ -> “the entered character is immediately returned without waiting for the enter key.” . Instead it WAITS for the Enter key.
    – there is no explanation on the main code line: leap=… – this is the hardest one to understand and needs good explanation. Every other line is trivial.
    – there are no sanity checks on the input. enter a character instead of a number and the program will say that it is a leap year:

    enter any yr: e
    the given year is leap year

    Check in e.g. https://www.onlinegdb.com/online_c_compiler#

Leave a Reply

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

English
Exit mobile version