ปีอธิกสุรทินประกอบด้วยอีกหนึ่งวันที่เพิ่มเข้ามาเพื่อให้ปีปฏิทินตรงกับปีดาราศาสตร์
ปีที่หารด้วย 4 ลงตัวเรียกว่าปีอธิกสุรทิน อย่างไรก็ตาม ปีที่หารด้วย 100 ลงตัวไม่ใช่ปีอธิกสุรทินในขณะที่ปีนั้นหารด้วย 400 ลงตัว
โปรแกรมตรวจสอบปีอธิกสุรทินหรือไม่มีดังนี้ -
ตัวอย่าง
#include<iostream>
using namespace std;
int main() {
int year = 2016;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
cout<<year<<" is a leap year";
else
cout<<year<<" is not a leap year";
return 0;
} ผลลัพธ์
2016 is a leap year
ในโปรแกรมข้างต้น ถ้าปีหารด้วย 4 ลงตัวและไม่หารด้วย 100 แสดงว่าเป็นปีอธิกสุรทิน นอกจากนี้ หากปีหารด้วย 400 ลงตัว ถือเป็นปีอธิกสุรทิน
สิ่งนี้แสดงให้เห็นโดยข้อมูลโค้ดต่อไปนี้
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) cout<<year<<" is a leap year"; else cout<<year<<" is not a leap year";
โปรแกรมตรวจสอบว่าปีเป็นปีอธิกสุรทินหรือไม่สามารถเขียนโดยใช้คำสั่ง nested if ได้ดังนี้ −
ตัวอย่าง
#include <iostream>
using namespace std;
int main() {
int year = 2020;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
cout << year << " is a leap year";
else
cout << year << " is not a leap year";
} else
cout << year << " is a leap year";
} else
cout << year << " is not a leap year";
return 0;
} ผลลัพธ์
2020 is a leap year
ในโปรแกรมข้างต้น ถ้าปีที่หารด้วย 4 ลงตัว ก็จะถูกตรวจสอบว่าหารด้วย 100 ลงตัวหรือไม่ ถ้าหารด้วย 100 ลงตัว จะตรวจสอบว่าหารด้วย 400 ลงตัวหรือไม่ ใช่ แสดงว่าปีนั้นคือ ปีอธิกสุรทิน มิฉะนั้นก็ไม่ใช่ ถ้าปีนั้นหารด้วย 100 ไม่ได้ แสดงว่าเป็นปีอธิกสุรทิน ถ้าปีนั้นหารด้วย 4 ไม่ได้ก็ไม่ใช่ปีอธิกสุรทิน
สิ่งนี้แสดงให้เห็นโดยข้อมูลโค้ดต่อไปนี้ -
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
cout << year << " is a leap year";
else
cout << year << " is not a leap year";
} else
cout << year << " is a leap year";
} else
cout << year << " is not a leap year";