Program 5 - CSU1128 - CSE 2026 - Shoolini University

Program to check if an entered year is leap or not without if else statement

Explanation

The following code is written in the C programming language and checks if a year entered by the user is a leap year or not, without using an if-else statement. The code uses the ternary operator ? : to achieve this. The code starts by including the standard input-output library stdio.h which contains functions like printf and scanf that are used in the code.


#include <stdio.h>

The main function is then defined using the int main() syntax, which is the entry point for all C programs. Inside the main function, an integer variable d is declared to store the year entered by the user.


int main()
{
    int d;

The code then prints a message to the user asking them to enter a year. This is done using the printf function which takes a formatted string as an argument and displays it on the screen.


printf("\n\n Program to check if an entered year is leap or not without if else statement. \n\n");
printf("Enter an year - (Example: 2100) - and press enter: ");

The scanf function is then used to get the year entered by the user. The %d in the format string of the scanf function specifies that the input should be an integer, and the &d symbol is the memory address of the d variable, which stores the entered year.


scanf("%d", &d);

The code then prints a message saying The entered year is, followed by the result of the leap year check. This check is performed using the ternary operator ? :, which takes two expressions and evaluates to the expression on the left of the : if the condition in the middle is true, and to the expression on the right if it is false.


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

The condition being checked in the ternary operator is whether the entered year is divisible by 4 or divisible by 400. If either of these conditions are true, then the year is a leap year and the code outputs a leap year. If both conditions are false, the code outputs not a leap year. The % symbol is the modulo operator in C, which gives the remainder of a division operation.

Finally, the main function returns a value of 0 to indicate that the program has executed successfully.


return 0;
}

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    int d;
    printf("\n\n Program to check if an entered year is leap or not without if else statement. \n\n");
    printf("Enter an year - (Example: 2100) - and press enter: ");
    scanf("%d", &d);
    printf("\n The entered year is");
    (d % 4 == 0 || d % 400 == 0) ? printf(" a leap year.\n") : printf(" not a leap year.\n");
    return 0;
}

Output

Program to check if an entered year is leap or not without if else statement.

Enter an year - (Example: 2100) - and press enter: 2000

 The entered year is a leap year.