Make your first for loop like so :
for ( i=2;i<=40;i++)
Then make your second for loop like so
for(i2=2; i2<i; i2++)
The main problem is that you are printing inside the loop. Print outside.
You overall loop could look like this :
int i = 0, i2 = 0;
for(i = 2; i <= 40; i++){
for(i2=2; i2<i; i2++){
if(i % i2 == 0 && i != 2) break;
}
if(i2 == i)print "i" is prime;
else print "i" is not a prime;
}
See how the print is outside the second loop.