What is the time complexity of the piece of code given below
The loop executes at most $O(\sqrt n)$ times because i is incremented in every iteration of the loop. You claim that it is also executed at least $\Omega(\sqrt n)$ times. This is not true because it has an early exit (the return statement). For example for even numbers the function returns during the first iteration. 

If you recall the definition of $\Omega$, we have $f(n) \in \Omega(g(n))$ if there is some constant $n_0$ such that for all $n&gt;n_0$ we have $c*g(n) &lt; f(n)$ for some constant $c$. By this definition the runtime of your program can't be $\Omega(\sqrt n)$ because the loop makes only one iteration for infinitely many inputs (that is, you can't find a suitable $n_0$).

You can of course restrict your analysis to the worst case ($n$ is a prime number). Then you are correct in saying that the runtime is $\Theta(\sqrt n)$. There is a question about the relation of $O$, $\Omega$, best and worst case in case you find this confusing.