Q:

Moment of inertia of solid cylinder

I want to work out the moment of inertia of a solid cylinder of radius $r$, length $l$ and mass $M$ about an axis through the centre of the cylinder. 
My approach was to line the central axis of the cylinder with the $x$-axis and consider a small cylindrical element of thickness $dx$. Then my mass element would be $dm = \rho \pi r^2 dx$, where $\rho$ is the mass per unit volume (density). 
Using the formula for moment of inertia and integrating from $0$ to $l$, I then find the answer to be $Mr^2$. Now that is wrong, there should be a factor of $\frac{1}{2}$ in there. But I don't understand why. Some solutions I've seen online consider concentric disks, but I don't understand why this method isn't working. 

A:

The $dm$ you have calculated is incorrect. The radius will vary. Which you have assumed constant. So ,
(https://i.stack.imgur.com/f4VjF.png)
[r1=x is the distance of each element from axis]
$$dm=\rho 2\pi x dx l$$.
$$\rho=\frac{M}{\pi R^2l}$$
$$dI=(dm) x^2$$
So,
$$I=\int_0^R \frac{2M}{R^2}x^3$$
$$I=\frac{MR^2}{2}$$

