H2X:
To stabilize the compound with hydrogen, two atoms of X are required. Therefore, the valency of X in this compound will be two.
CX2:
To stabilize the compound with carbon, two atoms of X are required. Thus, the valency of X in this compound is two.
XO2:
To stabilize this compound with oxygen, four atoms of X are required. Thus, the valency of X in this compound is four.
XO3:
To stabilize this compound with oxygen, six atoms of X are required. Thus, the valency of X in this compound is six.