Ayasha, This problem can be solved by both Set theory and Venn diagram. I am showing the set theory method.
For 3 sets M, S and E, total number of elements or students here, will be n(M U S U E)=n(M)+n(S)+n(E)-n(M cap E)-n(S cap E)-n(M cap S)+n(M cap S cap E). (Here cap means the intersection symbol). Now putting the values we get 20+18+16-7-7-9+3=34. SO, 34 students take at least one course. So 40-34=6 students do not take any course.