Use nan before b, p, d, t, n, r, and z. Use nani before all other letters.
Nan and Nani
Generally nan, or nani is used for which ever flows better. However, Nani is used when wanting to be more polite and formal. Other situations may require getting a feel for using nan versus nani, or memorization.
In general, if the following word begins with a syllable from the t, n, and d groups. Nan is used.
Common usages: Nan is used before particles [no]/の, [de]/で and verb da/desu.
Nani is used Before [ka]/か and [ni]/に,
If nan was used before ka/か, it would sound like the word nanka, which means "things like" or "something like that" which is also considered rude. Nanika is instead used. Both nanka and nanika mean "something", but nanika is polite.
If nan was used in the case of ni/に, it would produce nanni なんに. This sounds similar to nannimo なんにも, which means "nothing at all". Generally mo/も、to indicate nothing, or absence. This could confuse a listener, waiting for a [mo] to be uttered.
Note that 何 will be either なに or なん depending on where it is used.
instead of anatano namae wa nan desuka I think it is more ploite to say onamae wa nandesuka
Ex 4870/71 the use of あなた seems odd to me as it seems to be trying to impose unnecessary and perhaps misleading English style on Japanese but I am not a native J-speaker and am a novice - any comments from native speakers?