Let $q_{0}$ be the initial charge on a capacitor. Let the charged capacitor be connected to an inductor of inductance $L$. This $LC$ circuit will sustain an oscillation with frequency $\omega = \frac{1}{\sqrt{LC}}$.
At an instant $t$,the charge $q$ on the capacitor and the current $i$ are given by:
$q(t) = q_{0} \cos(\omega t)$
$i(t) = -q_{0} \omega \sin(\omega t)$
Energy stored in the capacitor at time $t$ is:
$U_{E} = \frac{q^{2}}{2C} = \frac{q_{0}^{2}}{2C} \cos^{2}(\omega t)$
Energy stored in the inductor at time $t$ is:
$U_{M} = \frac{1}{2} L i^{2} = \frac{1}{2} L (q_{0} \omega \sin(\omega t))^{2} = \frac{1}{2} L q_{0}^{2} \omega^{2} \sin^{2}(\omega t)$
Since $\omega^{2} = \frac{1}{LC}$,we have:
$U_{M} = \frac{1}{2} L q_{0}^{2} \left(\frac{1}{LC}\right) \sin^{2}(\omega t) = \frac{q_{0}^{2}}{2C} \sin^{2}(\omega t)$
Sum of energies:
$U = U_{E} + U_{M} = \frac{q_{0}^{2}}{2C} \cos^{2}(\omega t) + \frac{q_{0}^{2}}{2C} \sin^{2}(\omega t)$
$U = \frac{q_{0}^{2}}{2C} (\cos^{2}(\omega t) + \sin^{2}(\omega t)) = \frac{q_{0}^{2}}{2C}$
Since $q_{0}$ and $C$ are constants,the total energy $U$ is constant in time.