Bash Script to Check Leap Year with Output and Explanation
Use a Bash script with conditions:
if (( year % 400 == 0 )) || (( year % 4 == 0 && year % 100 != 0 )); then echo "Leap year"; else echo "Not leap year"; fi to check leap years.Examples
Input2000
OutputLeap year
Input1900
OutputNot leap year
Input2024
OutputLeap year
How to Think About It
To check if a year is leap, first see if it divides evenly by 400. If yes, it is leap. If not, check if it divides evenly by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
Algorithm
1
Get the year input from the user2
Check if the year is divisible by 400; if yes, it is a leap year3
Else check if the year is divisible by 4 but not by 100; if yes, it is a leap year4
Otherwise, it is not a leap year5
Print the resultCode
bash
#!/bin/bash read -p "Enter a year: " year if (( year % 400 == 0 )) || (( year % 4 == 0 && year % 100 != 0 )); then echo "Leap year" else echo "Not leap year" fi
Output
Enter a year: 2024
Leap year
Dry Run
Let's trace the year 1900 through the code
1
Input year
year = 1900
2
Check divisibility by 400
1900 % 400 = 300 (not zero)
3
Check divisibility by 4 and not by 100
1900 % 4 = 0 and 1900 % 100 = 0 (fails second condition)
4
Result
Print 'Not leap year'
| Step | Condition | Result |
|---|---|---|
| 1 | year = 1900 | Input received |
| 2 | 1900 % 400 == 0 | False |
| 3 | 1900 % 4 == 0 && 1900 % 100 != 0 | False |
| 4 | Print result | Not leap year |
Why This Works
Step 1: Divisible by 400
If a year divides evenly by 400, it is always a leap year, so we check this first.
Step 2: Divisible by 4 but not 100
If not divisible by 400, the year must be divisible by 4 but not by 100 to be leap.
Step 3: Otherwise not leap
If neither condition is met, the year is not a leap year.
Alternative Approaches
Using expr command
bash
#!/bin/bash read -p "Enter a year: " year if [ $(expr $year % 400) -eq 0 ] || { [ $(expr $year % 4) -eq 0 ] && [ $(expr $year % 100) -ne 0 ]; }; then echo "Leap year" else echo "Not leap year" fi
Uses external expr command for arithmetic, less efficient but compatible with older shells.
Using bc for arithmetic
bash
#!/bin/bash read -p "Enter a year: " year is_leap=$(echo "($year % 400 == 0) || (($year % 4 == 0) && ($year % 100 != 0))" | bc) if [ "$is_leap" -eq 1 ]; then echo "Leap year" else echo "Not leap year" fi
Uses bc calculator for arithmetic, useful if shell arithmetic is limited.
Complexity: O(1) time, O(1) space
Time Complexity
The script performs a fixed number of arithmetic operations and condition checks, so it runs in constant time.
Space Complexity
Only a few variables are used, so space usage is constant.
Which Approach is Fastest?
Using Bash's built-in arithmetic (( )) is fastest and simplest compared to external commands like expr or bc.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bash arithmetic (( )) | O(1) | O(1) | Modern Bash scripts, best performance |
| expr command | O(1) | O(1) | Older shells compatibility |
| bc calculator | O(1) | O(1) | Complex arithmetic or limited shell arithmetic |
Use double parentheses (( )) in Bash for clean arithmetic and logical conditions.
Forgetting to check the 'not divisible by 100' condition causes incorrect leap year results.