That's a very good question indeed :)
And yes , there are some rules on it :
1- After number 3 to number 10 , you write the name on the opposite of the number , masculine or feminine
, like :
عندي تسعة أقلام
ثلاث تفاحات
2- After numbers 11 and 12 , you put the name as the same as the numbers , masculine or feminine , ( in the two parts ) , like : عندي أحد عشر كتابا
عندي إحدى عشرة تفاحة
عندي اثتا عشر بيتا
عندي اثنا عشرة تفاحة
3- From number 13 , to number 19 , you put the first part of the number on the opposite of the name , masculine or feminine , and you put the second part of the number on the same of the name , masculine or feminine , like :
عندي ثلاث عشرة تفاحة
عندي ثلاثة عشر قلما
عندي تسعة عشر بيتا
عندي تسع عشرة ورقة
--------------
Is that clear for you ? or you want more explanation ? :)