Is the lottery gambling even if the money goes to charity?
Yes. The lottery is gambling, and a charitable purpose does not change that. Anyone who wants to help can give directly, and then the whole gift reaches those in need.
Islam rejects gambling in every form. It does not only harm the person who loses money. It also feeds the hope of quick and easy wealth. Someone who gets used to that hope puts less effort into honest work, the kind that builds something patiently, step by step. In the end, gambling holds back the economy of a whole country.
The second verse above admits that gambling can bring some benefit. Part of the money may go to good causes. Yet the harm is greater than the benefit. Of all the money people spend on tickets, only a small share is left for charity. Much of it goes to the state, to the organisers and elsewhere.
Intention matters too. A person who buys a ticket is really hoping for the big win. Charity is often just the excuse. God sees into the heart. What comes from the desire for gain is not true charity, and one cannot expect a reward from God for it.
There is a simpler and better way. If everyone who buys lottery tickets gave that money directly to the poor and the sick, far more would reach them. The hadith above shows that God makes even a small gift from honest earnings grow. Such a gift needs no prize to tempt anyone into giving.