Quote:
for(int i=2;i<=num/2;i++)
Why we divide num/2 as loop break?
We don't.
This code is a loop that breaks when
i
get over
num/2
.
Think about how you check if a number is prime, what do you do, when do you stop ?
Take a sheet of paper, a pencil and write down what you do to check 31.
Your program is very buggy, use the debugger to see what the code is doing.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[
^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[
^]
[Update]
By the way, rather than trying a code that you have been given and don't work,
try to write your own, it can't be worse.
A great lecture to learn C
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[
^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[
^]