“Have you not seen that Allah created the heavens and the earth for a reason”

Allah(swt) is saying here there is a purpose in our creation that can be observed by us.

“I did not create jinn and humans except to worship Me” from this verse we know now our purpose is too worship him.

Two questions arise when I first read these verses:
1)Why did a independent creator with no desires create us to worship him?

2) Why does Allah(swt) make it obligatory to pray to him? Why does he make it obligatory to confess his greatness and to glorify him?

1- Because he wanted to show us mercy. Not because he has any needs, but just to show us mercy. And to achieve the highest mercy we worship Him.

2- By praying to Him and confessing His greatness, we connect ourselves to the source of all goodness in the universe. If we don’t, we will disconnect ourselves and be doomed. Allah is merciful so he wants us to succeed, not to be doomed.