In: Computer Science
C++
A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
The C++ code sniffet of main.cpp is:
#include <iostream>
void NextLeapYear(int *year)
{
int temp = *year; //to prevent any mid changes in variable.
if(temp < 0) //negative entry check
{
*year = 4;
return;
}
temp = ((temp/4)+1)*4; // this is most probable leap yr next to year provided.
//Nested if statements check if temp contains a year which is multiple of 100 and not a multiple of 400 like 1900 which is not a leap year.
if(temp%100 == 0)
{
if(temp%400 != 0)
{
temp += 4;
}
}
*year = temp;
return;
}
int main() {
int yr;
std::cout << "Hello world!\n";
std::cout << "Enter a year: ";
std::cin >> yr;
NextLeapYear(&yr);
std::cout << "Next Leap Year is: " << yr;
return 0;
}
The function NextLeapYear() is:
void NextLeapYear(int *year)
{
int temp = *year; //to prevent any mid changes in variable.
if(temp < 0) //negative entry check
{
*year = 4;
return;
}
temp = ((temp/4)+1)*4; // this is most probable leap yr next to year provided.
//Nested if statements check if temp contains a year which is multiple of 100 and not a multiple of 400 like 1900 which is not a leap year.
if(temp%100 == 0)
{
if(temp%400 != 0)
{
temp += 4;
}
}
*year = temp;
return;
}
Sample Output:
Hope it helps.