Can someone explain how this program works?

Heres the code my friend wrote to determine whether a year is leap year or not.

#include <iostream>
using namespace std;

int remainder (int num1, int num2)
{
int divide = num1 % num2;
return divide;
}

int main()
{
int year = 0;
bool output = 0;
cout << "Enter a Year: ";
cin >> year;
int r1 = remainder (year,4);
int r2 = remainder (year,100);
int r3 = remainder (year,400);
if (r1 == 0)
{
if (r2 == 0)
{
if (r3 == 0)
{
output = 1;
}
else
{
output = 0;
}
}
else
{
output = 1;
}
}
else
{
output = 0;
}
if (output == 0)
{
cout << "The year " << year << " is not a leap year." << endl;
}
else
{
cout << "The year " << year << " is a leap year." << endl;
}
return 0;
}



My question: i ondt understand how the part above int main works, since we dont have any values for int1 or int2.
-can someone explain to me how the rx-remainder (year,4) works[i understnad you have to divide the year by 4, or by 400 to see if its leap or not]-but i dont understand the operation going on
You should use the code tags to post code, so it's formatted and easily readable.

int divide = num1 % num2; // will return the remainder (0 if there isn't one)

In main, the remainder() function is called. The remainder() function is returning an int containing the divide value and using it in the if/else's in main() to determine leap year. The confusion may be that int divide might be better defined as int remain (or something like that).
Last edited on
what are code tags and how do i use them?
On the right of your reply box, the # button. Write between the [ code ] and [ /code ] .
Topic archived. No new replies allowed.