Fourier cosine series of $\sin x$

The question I have been given states;

Consider the function $f:(0,\pi) \rightarrow \mathbb{R}$ defined by $x\longmapsto \sin x$

Show that the Fourier cosine series (i.e. the Fourier series of the even extension of $f$) is given by

$$\sin x\sim \frac{2}{\pi}-\sum_{n=2}^{\infty}\frac{2(1+(-1)^n)}{\pi(n^2-1)}\cos nx$$

Now I know that $f(x)\sim\frac{a_0}{2}+\sum_{n\in\mathbb{N}}a_n\cos nx$

So far I have gotten $a_0=\frac{4}{\pi}$ and I know the equation I must solve for $a_n$ is

$$a_n=\frac{2}{\pi}\int_0^\pi \! \sin x \cos nx \, \mathrm{d}x$$

My next step is to use integration by parts to get

$$=\frac{2}{\pi}((-1)^{n+1}-1)-n\int_0^\pi \cos x \sin nx$$

However, I am stuck from here.

You need to integrate by parts a second time. Let

$$I := \int \sin x \cos nx \, \operatorname{d}\!x$$

As you have done, we apply integration by parts: $u=\cos nx$ and $\operatorname{d}\!v = \sin x$ meaning that $\operatorname{d}\!u = -n\sin nx$ and $v=-\cos x$. Hence

$$I = -\cos x \cos nx - n \int \cos x \sin nx \, \operatorname{d}\!x$$

We now need to work out the integral on the right. Let $I = -\cos x \cos nx - nJ$ where $$J = \int \cos x \sin nx \, \operatorname{d}\!x$$

Integrating by parts a second time: $u = \sin nx$ and $\operatorname{d}\!v = \cos x \, \operatorname{d}\!x$. Meaning that $\operatorname{d}\!u = n\cos nx \, \operatorname{d}\!x$ and $v=\sin x$. Hence

$$J = \sin x \sin nx - n\int \sin x \cos nx \, \operatorname{d}\!x$$

The integral on the right looks familiar! It was our original $I$. Hence: $J = \sin x \sin nx - nI$. From the first integration by parts we got $I = -\cos x \cos nx - nJ$. Putting these together:

$$\begin{eqnarray*} I &=& -\cos x \cos nx - n J \\ \\ &=& -\cos x \cos nx - n(\sin x \sin nx - nI) \\ \\ &=& -\cos x \cos nx - n \sin n \sin nx + n^2 I \end{eqnarray*}$$

Solving this for $I$ gives:

$$I = \frac{\cos x \cos nx + n \sin x \sin nx}{n^2-1}$$

All you need to now is apply your limits, i.e.

$$\int_0^{\pi} \sin x \cos nx \, \operatorname{d}\!x = \left[\frac{\cos x \cos nx + n \sin x \sin nx}{n^2-1}\right]_0^{\pi}$$

You can also use the trigonometric identity:

$\sin x \cos nx = (1/2)[\sin[(n + 1)x] − \sin[(n − 1)x]]$

to avoid integration by parts.

For the integral in the last inequality you obtained try integration by parts one more time. You'll get the first integral that you want to find. Then solve the last equality for this integral (like a linear equation). I think this helps.