Find prime number using for loop

import java.util.*;

public class Try {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	int temp = 0;
	int i = 2;
	for (i = 2; i <= n - 1; i++)
		;
	{
		if (n % i == 0) {
			temp = temp + 1;
		}
	}
	if (temp>1) {
		System.out.println(" prime no");
	} else {
		System.out.println("NON prime no");
	}
}

}

here input given is 7or 9 both gives non prime number y so

Hi @mannarun09_868823821c6da921

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int div=2;
while(div<n){
if(n%div==0){
System.out.println(“not prime”);
return;
}
div++;
}
System.out.println(“prime”);

}

}

1 Like

Thanks.
But why not with for loop i need to find out the mistake their. if possible do so…

@mannarun09_868823821c6da921
There is no difference between using for and while loop if i just write all the conditions in for loop it will also run fine

Agree but why still its not running. still error with similar condition as u gave show below with error
THE CODE:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n =s.nextInt() ;
int temp = 0;
for (int Div= 2; Div <= n - 1; Div++);
{
if (n % Div == 0) {
temp = temp + 1;
}
}
if (temp==0) {
System.out.println(n+ " prime no");
} else {
System.out.println(n+ “NON prime no”);
}
}

}

NOW ERROR:-
/Main.java:9: error: cannot find symbol
if (n % Div == 0) {
^
symbol: variable Div
location: class Main
1 error

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

M still the same regarding that problem. plz explain it briefly…

Please initialize the div variable outside the for then it will run fine.

done but still same either u give 11 or 10 both gives same answer.

please
1 write a program to find weather the number is prime or not in for loop… And
2 how to count total no that are prime between 1to 100.