Although this is C program. But prime number logic will be same for C and Java both
Prime number
Each natural number that is divisible only by 1 and itself is prime.
Also, 2 is the first prime number.
For example, we want to test that number 100 is a prime number or not. we can do a trial division to test the primality of 100.
Let’s look at all the divisors of 100:
2, 4, 5, 10, 20, 25, 50
Here we see that the largest factor is 100/2 = 50. This is true for all n: all divisors are less than or equal to n/2.
So here condition i<=n/2 condition is correct. Since we need to test divisors up to n/2 only.
Please check the Wiki link for more detail
https://en.wikipedia.org/wiki/Primality_test
Second example
Similarly, for 11 you would check all integers smaller than 5.5, i.e. 1, 2, 3, 4 and 5.
To find a number is prime, Why checking till n/2 is better. What is the reason for avoiding numbres in second half of n
1
solved Prime number Logic, n/2 condition in a loop