See attached. We can draw a right triangles contained within both shapes.
The triangle has height x (same as the cylinder), base 2r (same as the diameter of the cylinder), and hypotenuse 6 (same as the diameter of the sphere).
Since it's a right triangle, the Pythagorean theorem holds and we have
x² + (2r)² = 6²
which we solve for r to get
x² + 4r² = 36
4r² = 36 - x²
r² = 9 - x²/4
r = √(9 - x²/4)
Then the volume of the cylinder, whose height is x and whose base has radius r, is
π r² x = π (9 - x²/4) x = (36x - x³) π/4