Originally the OP said "without any built-in functions". Mod is not a function, it's an operator. So the Mod technique is perfectly valid. If the teacher argues that Mod is also excluded, then you must point out to him that in that case you also cannot use +, - *, /, or \ because they are ALL operators, so his question is invalid.
Doing it any other way is a waste of time anyways, and I don't see what it could possibly teach anyone, except how to do it the wrong way.
If it was me, I would also tell him how much of a ****ty teacher he is for even asking that question in the first place, but I was kind of an ******* in school when I was faced with people who obviously didn't know the first thing about what they were teaching...