#include<stdio.h>

int main()

{

int num,i;

printf(“enter the number”);

scanf("%d",&num);

for(i=2;i<=num-1;i++);

{

if(num%i==0)

{

printf(“the number is not prime”);

break;

}

if(i==num)

printf(“prime number”);

}

return 0;

}

There are many mistakes in this program wrt logic and syntax.

- for(i=2;i<=num-1;i++);{ - Remove the semicolon before {
- Your loop terminates at num-1 so if a given number is a prime number it will never call the 2nd ‘if’ statement. Instead, you may use a bool variable and then use it after the ‘for’ loop ends.
- iterate from i=2 to num, first check if(i == num) for the number being a prime no. and then divisibility. That is because if a num is prime then it will evaluate both the if condition as true.
- You have not handled the special case when num = 1

Modification in your code -

```
#include<stdio.h>
int main(){
int num,i;
//printf("enter the number");
scanf("%d",&num);
if(num == 1){
printf("the number is not prime");
}
else{
for(i=2;i<=num;i++){
if(i==num){
printf("prime number");
}
else if(num%i==0){
printf("the number is not prime");
break;
}
}
}
return 0;
}
```

Alternate implementation -

```
int main(){
int num,i;
//printf("enter the number");
scanf("%d",&num);
int f = 1;
for(i=2;i<num;i++){
if(num%i == 0){
f = 0;
break;
}
}
if(f && num != 1){
printf("The number is prime");
}
else{
printf("The number is not prime");
}
return 0;
}```
```

The same programming idea as you are attempting, implemented in Python:

```
num = int(input('enter the number:'))
for tfac in range(2,num):
if num%tfac == 0:
print('the number is not prime')
break
else:
print('prime number')
```

The `else:`

clause here activates only if the `for`

loop terminates without a break. (The `for`

loop there assigns `n-2`

values to `tfac`

, starting at `2`

and ending at `n-1`

; that’s a peculiarity of how `range`

is defined).

Note this algorithm is **wrong** for an input of `1`

, which is not a prime number.

*Algorithmically* it’s better to take advantage of the fact that if a number has factors (is composite) then at least one such factor is less than or equal to the square root of the number, because if k divides n then there is a number j such that jk = n, and if both j and k were greater than the square root of n then jk would be greater than n.

```
num = int(input('enter the number:'))
tfac = 2
while tfac*tfac <= num:
if num%tfac == 0:
break
tfac += 1
if tfac*tfac > num and num > 1:
print(num, "is a prime number")
else:
print(num, "is not prime")
```

There are ways this concept could be further improved, and a large variety of other prime finding algorithms.

bro yr 2nd point is wrong check again

thanks man,but could you please elaborate testing i==num before divisibility.

Bro in your for loop you have set the bound for the variable ‘i’ from 2 to (num-1) inclusive. If you add the if(i==num) condition inside your for loop, it would never be satisfied because the max value i can take inside the for loop is (num-1). So ‘i’ will never be equal to ‘num’ inside the loop.

You would want to put the if(i==num) condition after the end of the ‘for’ loop and as the folks above me have pointed out, avoid syntactical errors in your code and you shall be good, although this approach is inefficient because the the variable ‘i’ need not run in the range(num/2 <= i <num-1 ) becuase in this range no value of ‘i’ divides num.

Keep practicing dude and have a great day