Consider the capacitor with the capacitance C, the charge on the plates q, and the potential difference between the plates U.
Then the energy stored in the capacitor is `E=(qU)/2` .
Since by definition the capacitance is `C=q/U` , the formula for energy can be rewritten in terms of C and U:
`q =CU` , so `E=((CU)U)/2=(CU^2)/2`
Alternatively, it can be written in terms of q and C:
`U = q/C` , so `E=q/2 (q/C) = q^2/(2C)`
`E=(qU)/2 = (CU^2)/2 = q^2/(2C)`