# How to calculate the total charge of a sphere which has a none uniform charge distribution?

If I am given a certain funciton which describes the volume charge density within the sphere with respect to the radius (ρ(r)),how can I calculate the total charge within the sphere.My idea was by calculating the total charge of many infinitesimally small and thin circles. Initially, I though that I could use $$\int (πy^2)*(ρ(r)) \, dr$$ Where $$\ x^2 + y^2 = r^2$$ Because this idea it didnt work i thought of using the areas of small spheres times the volume charge density $$\int (4πr^2)*(ρ(r)) \, dr$$ It worked by I am not sure if this is the proper way or if it was pure luck due to the numbers that i got the answer right.

Even if the 2nd way is right can someone give me some more information in order to understand it better, or is there a better solution which is more understandable.

Assuming that $$\\rho_r\$$ is only a function of $$\r\$$ and not a function of $$\x,y,z\$$, your first thought about the thin shell is about right, except misapplied due to ignoring $$\z\$$. Instead, you just take the shortcut that you know the surface area at $$\r\$$ is $$\4\pi\,r^2\$$ and that it's thickness is obviously $$\\text{d}r\$$ (you are only certain that $$\\rho_r\$$ is exactly true for an infinitesimally thin shell, which of course is $$\\text{d}r\$$ thick.) So the volume of that thin shell is $$\4\pi\,r^2\:\text{d}r\$$.
Given that $$\\rho_r\$$ is in the units of $$\\frac{\text{charge}}{\text{volume}}\$$, multiplying it by the volume of the thin shell provides the charge in that thin shell. So it is correct to multiply the volume of the thin shell by your $$\\rho_r\$$ factor to get charge in that thin shell.
I might have just as well have written your last equation as $$\\int 4\pi\,r^2\:\text{d}r\:\rho_r\$$ or $$\\int \rho_r\:4\pi\,r^2\:\text{d}r\$$. It's all the same thing. But yes, I think you used the right equation at the end.