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
 A: 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$.
A: The product-to-sum identity for $\sin \alpha \sin \beta$ (for example) gives
$$\int_0^\pi \sin k x \sin nx \,dx = \frac{1}{2} \int_0^\pi [\cos (k - n) x - \cos(k + n) x] \,dx.$$
Henceforth assume $k, n$ are integers. For $k \neq \pm n$, symmetry gives $\int_0^\pi \cos (k \mp n) x \,dx = 0$ (compute this directly to see why the condition $k \neq \pm n$ is necessary) and hence $$\int_0^\pi \sin k x \sin nx \,dx = 0 ,$$ but if $k = \pm n$, then $k \mp n = 0$ and $\cos (k \mp n) x = 1$. So, for $k = \pm n \neq 0$, $$\int_0^\pi \sin k x \sin nx \,dx = \pm \frac{\pi}{2} ,$$ and if $k = n = 0$, then $$\int_0^\pi \sin k x \sin nx \,dx = 0 .$$
The analysis of $$\int_0^\pi \cos k x \cos nx \,dx$$ is similar (but NB that for $k = n = 0$, $\int_0^\pi \cos k x \cos nx \,dx = \pi$).
A: Let $$I_1 = \int_0^\pi \sin(kx)\sin(nx)dx  $$
$$I_2 = \int_0^\pi \cos(kx)\cos(nx)dx  $$
$$ I_3 = I_1 + I_2  = \int_0^\pi \cos((k-n)x)dx$$
$$I_4 = I_2 - I_1 =  \int_0^\pi \cos((k+n)x)dx$$
Provided that $n$ and $k$ are both integers , both $I_3$ and $I_4$ will vanish unless $k=\pm n$, in which case one still vanishes and the other equals $\pi$.
A: $cos(k\theta)cos(n\theta)={1\over 2}[cos((n-k)\theta)+cos(n+k)\theta)$
$\int_0^{\pi}cos((n+k)\theta)={1\over{n+k}}sin(n+k)\theta]_0^{\pi}=0$,
if $n\neq k$, $\int_0^{\pi}cos((n-k)\theta)={1\over{n-k}}sin(n-k)\theta]_0^{\pi}=0$.
A: The cause is something referred to as the orthogonality of the functions sine and cosine. We define two functions, $f,g$ to be orthogonal on an interval $[a,b]$ if $\int_a^b fg = 0$. The cosine and sine functions exhibit this property over an interval whose length is an integer multiple of their period.
I'll cover showing this in the cosine case. The sine case is fairly similar.

If you try getting the antiderivative in the cosine case for $k \ne n$, we begin with
$$\int \cos(kx)\cos(nx) dx$$
Using one of the product to sum formulas (here's a handy trig reference sheet by the way), we see
$$\int \cos(kx)\cos(nx) dx = \frac 1 2 \left( \int \cos((k-n)x)dx + \int \cos((k+n)x)dx \right)$$
Evaluating the antiderivatives, we see then 
$$\int \cos(kx)\cos(nx) dx = \frac 1 2 \left( \color{blue}{\frac{1}{k-n}} \sin((k-n)x) + \frac{1}{k+n}\sin((k+n)x) \right) + C$$
for constant of integration $C$. Notice how this is undefined if $k=n$ because of the fraction in blue. As a result, we have to derive the $k=n$ case separately. Thus, if $k=n$,
$$\int \cos(kx)\cos(nx) dx = \int \cos^2(kx) dx$$
Using one of the alternate forms of the half-angle formulas on that reference sheet, we see that
$$\int \cos^2(kx) dx = \frac 1 2 \int dx + \frac 1 2 \int \cos(2kx)dx$$
Evaluating the antiderivative, we get
$$\int \cos^2(kx) dx = \frac 1 2 x + \frac 1 2 \cdot \frac{\sin(2x)}{2k} = \frac 1 2 x + \frac 1 {4k} \sin(2x) + C$$
Thus we conclude:
$$\int \cos(kx)\cos(nx) dx = \left\{\begin{matrix}
\frac 1 2 \left( \frac{1}{k-n} \sin((k-n)x) + \frac{1}{k+n}\sin((k+n)x) \right) + C & k \neq n\\ 
\frac 1 2 x + \frac 1 {4k} \sin(2x) + C & k=n
\end{matrix}\right.$$
If we try to then evaluate the antiderivative over an interval $[a,a+2\pi]$, we get an interesting result. Note that there is a proof out there that the integral from $[a,a+p]$ for a function of period $p$ is equal to that of the integral over $[0,p]$, so it is sufficient to look at the integral over $[0,2\pi]$.
Evaluating at the respective bounds in the antiderivative, if $k,n$ are integers, you'll immediately realize that the sine of a multiple of $\pi$ is zero, and thus the first case is $0$. In the $k=n$ case, we simply get $\pi/2$ by a similar process. Thus,
$$\int_a^{a+2\pi} \cos(kx)\cos(nx) dx = \int_0^{2\pi} \cos(kx)\cos(nx) dx =\left\{ \begin{matrix}
0 & k \neq n \\ 
\pi/2 & k=n
\end{matrix}\right. $$
This also gives us that $\cos(kx)$ is orthogonal to $\cos(nx)$ whenever $k \neq n$. This proves to be useful in some higher mathematical settings, e.g. Fourier analysis.
