Proof of $\cos \theta+\cos 2\theta+\cos 3\theta+\cdots+\cos n\theta=\frac{\sin\frac12n\theta}{\sin\frac12\theta}\cos\frac12(n+1)\theta$
Here are the main steps.
You may write $$ \begin{align} \sum_{k=1}^{n} \cos (k\theta)&=\Re \sum_{k=1}^{n} e^{ik\theta}\\\\ &=\Re\left( e^{i\theta}\frac{e^{in\theta}-1}{e^{i\theta}-1}\right)\\\\ &=\Re\left( e^{i\theta}\frac{e^{in\theta/2}\left(e^{in\theta/2}-e^{-in\theta/2}\right)}{e^{i\theta/2}\left(e^{i\theta/2}-e^{-i\theta/2}\right)}\right)\\\\ &=\Re\left( e^{i\theta}\frac{e^{in\theta/2}\left(2i\sin(n\theta/2)\right)}{e^{i\theta/2}\left(2i\sin(\theta/2)\right)}\right)\\\\ &=\Re\left( e^{i(n+1)\theta/2}\frac{\sin(n\theta/2)}{\sin(\theta/2)}\right)\\\\ &=\Re\left( \left(\cos ((n+1)\theta/2)+i\sin ((n+1)\theta/2)\right)\frac{\sin(n\theta/2)}{\sin(\theta/2)}\right)\\\\ &=\frac{\sin(n\theta/2)}{\sin(\theta/2)}\cos ((n+1)\theta/2). \end{align} $$
Using the appropriate prosthaphaeresis formula,
$$\begin{align} \sin{\frac{\theta}{2}}\cos{k\theta} &=\frac12\left[\sin{\left(\frac{\theta}{2}+k\theta\right)}+\sin{\left(\frac{\theta}{2}-k\theta\right)}\right]\\ &=\frac12\left[\sin{\left(\left(k+\frac{1}{2}\right)\theta\right)}-\sin{\left(\left(k-\frac{1}{2}\right)\theta\right)}\right]. \end{align}$$
In this form, summation of $k$ now transparently telescopes:
$$\begin{align} \sum_{k=1}^{n}\sin{\frac{\theta}{2}}\cos{k\theta} &=\sum_{k=1}^{n}\frac12\left[\sin{\left(\left(k+\frac{1}{2}\right)\theta\right)}-\sin{\left(\left(k-\frac{1}{2}\right)\theta\right)}\right]\\ &=\frac12\left[\sin{\left(\left(n+\frac{1}{2}\right)\theta\right)}-\sin{\left(\left(1-\frac{1}{2}\right)\theta\right)}\right]\\ &=\frac12\left[\sin{\left(\left(n+\frac{1}{2}\right)\theta\right)}-\sin{\left(\frac{\theta}{2}\right)}\right]\\ &=\sin{\left(\frac{n\theta}{2}\right)\cos{\left(\frac{(n+1)\theta}{2}\right)}}. \end{align}$$
Now divide through by $\sin\frac{\theta}{2}$.
Hint : You have not taken the real part correctly as you have missed the $isin\theta$ in the denominator