Division Operators

While arithmetic operators +, -, * are pretty straightforward, division is tricky.

For now, we are only working with whole numbers (int type), and the division operator /, when applied to two int values, returns an int. So, 6 / 2 is 3, but 7 / 2 is also 3. It works the same way with negative numbers: -6 / 2 is -3, while -7 / 2 is also -3.

Of course, C has special types for fractional numbers, but it's a big topic and we'll cover it later. Let's stick to the int type for now!

Modulo operator

Another useful arithmetic operator is the modulo operator %, which divides one number by another and returns a remainder: 13 % 3 is 1, because 13 is 3 * 4 + 1. So to check if a is divisible by b, you can write if (a % b == 0). Note that the result of % can be negative, e.g. -7 % 2 is -1.

Leap years

Let's have a quick exercise. A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100. It means that years 1904, 1908, 1912, ..., 1996 were leap years, but 1900 was not, because 1900 is divisible by 100. At the same time, 2000 was a leap year, because 2000 is divisible by 400.

Implement int is_leap(int year):

#include <stdio.h>

int is_leap(int year) {
	/* write your code here */
	return 0;
}

int main() {
	printf("is 1896 a leap year? %d\n", is_leap(1896));
	printf("is 1900 a leap year? %d\n", is_leap(1900));
	printf("is 1904 a leap year? %d\n", is_leap(1904));
	printf("is 1996 a leap year? %d\n", is_leap(1996));
	printf("is 2000 a leap year? %d\n", is_leap(2000));
	printf("is 2004 a leap year? %d\n", is_leap(2004));
	printf("is 2025 a leap year? %d\n", is_leap(2025));
	return 0;
}

Did all the tests pass?