Why do $\int_{0}^{\pi}d\theta\cos k\theta \cos n\theta$ and $\int_{0}^{\pi}d\theta\sin k\theta \sin n\theta$ equal zero, except when $|k|=n$? I do not understand why for the two integrals below, the result is always $0$ unless $|k|=n$, in which case the result is $\pi/2$. 
$$\int_{0}^\pi d\theta\cos k\theta \cos n\theta \qquad\qquad
\int_{0}^\pi d\theta \sin k \theta \sin n \theta$$
I have tried using trigonometric identities to get a general solution but I had no luck understanding the nature of the integrals. If anyone can point out any hints or patterns, it would be much appreciated. Thanks

I will show you how to do the first one. The second one is analogous.
Recall the trigonometric identity $\cos(k\theta)\cos(n\theta) = \frac{1}{2}\left[\cos(k-n)\theta+\cos(k+n)\theta\right]$. Plugging this into the integral, you get
$$\frac{1}{2}\int_0^\pi \left[\cos(k-n)\theta+\cos(k+n)\theta\right]d\theta = \frac{1}{2}\left.\left(\frac{\sin (k-n)\theta}{k-n} + \frac{\sin (k+n)\theta}{k+n}\right)\right|_0^\pi = 0$$
But, be careful that the above is valid only for $|k| \ne n$, because otherwise you have $0$ in the denominator and the expression is undefined. It is a fairly common mistake to forget this.
For $k=n$, we have that $\cos(k\theta)\cos(n\theta) = \frac{1}{2}\left[\cos0+\cos(k+n)\theta\right] = \frac{1}{2}\left[1+\cos(2n\theta)\right]$. Evaluating the integral of this function you will get $\pi/2$.
Similarly, for $k=-n$, we have that $\cos(k\theta)\cos(n\theta) = \frac{1}{2}\left[1+\cos(2n\theta)\right]$ and the integral of this function again evaluates to $\pi/2$.
