Well, it's a little complicated (many details) but I'll try to make it easier..
Using numbers in a sentence is a compound called تمييز ( Accusative of specification ).
The Arabic word تَمْييز (pronounced tamyiiz) is the verbal noun of the Form II verb مَيَّزَ " to single out, distinguish, " and this is exactly what this use of the accusative case isdesigned to do.
And in this case, tamyiiz construction is used with numbers to specify more precisely what the measurement applies to.
* If the used number is 1 or 2, then the verb follows gender ( masculine with male/ feminine with female ), and this happenes whether they're "independent" (1,2) or part of a number 21,22,31,32,41,42..etc :
حضرَ طَالِب واحِدٌ -
حضَرتْ طالِبَةٌ واحِدة -
حضرَ طَالِبانِ اثْنَانِ -
حَضرَتْ طالِبتانِ اثنتَان -
حضرَ واحدٌ وعِشرونَ طالِبًا -
حضَرت إحدَى وعِشرونَ طالِبةً -
* If the used number is from 3 to 9 (single numbers) then it takes the opposite gender of the word, while the verb follows gender :
جاء ثلاثة طلابٍ -
جاءت ثلاث طالباتٍ -
*If the used number is 10, then it takes the opposite gender of the word and the verb is masculine:
غادر مقر الاجتماع عشرة رجال -
حضر الحفل عشر نساء -