By firstly knowing the capacity of all shells and then keeping the electron like wise and the precaution not to keep more than 8 in one except in noble gas

Electrons are filled according to the increasing energy level. The energy level is represented by K,L.M,N . The 1st energy level can accommodate only  2 electrons, the 2nd and 3rd energy level can accommodate 8 electrons.To write the electronic configuration, write the atomic no. 1 to 18, at first put 2 electrons for first energy level and then write for next level. for ex-
Atomic no. 16
1st level will have 2 electrons (electrons remaining 16-2=14)
2nd level will have 8 electrons out of 14 (electrons remaining 14-8 = 6)
3rd level will accommodate remaining 6 electrons, so the electronic configuration will be 2,8,6