Check prime if the given number is prime or not

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();

	while(n>2)
	{
		if(n%2==0)
		{
			System.out.println("Prime");
		}
		else
		System.out.println("Not Prime");
	}

}

}
error:timelimit exceeded(wall time):aborting command
command terminated with signal 15 what i have to do sir to run testcases

@karthik1989photos,
your approach is wrong. First, there is no breaking condition with the while loop. Second, you are marking all even numbers >2 as prime numbers. Which is wrong.
2,3,5,7,11,13,17,19 and so on, these are the prime numbers.

Any number which has only two divisors, one divisor is the number itself and other divisor is 1, is called as Prime number.

Algorithmn

  • Take input of the number.
  • put a loop from 2 to that number.
  • If the number gets divided by any number in the loop, that means the number is not Prime.
  • Othewise, Prime.

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(i=2;i<=n;i++)
{
if(n%i==0)
{
System.out.println(“Not Prime”);
}
else
System.out.println(" Prime");
}

}

}
again showing sam kind of error

@karthik1989photos.
i is not declared and remove the space while printing “Prime”

in for loop i have declared i .import java.util.*;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i;
for(i=2;i<=n;i++)
{
if(n%i==0)
{
System.out.println(“Not Prime”);
}
else
System.out.println(“Prime”);
}

}

}
since showing timelimit error

@karthik1989photos,

If the input is 5, your code will give the output:
Prime
Prime
Prime
Not Prime

Now, for this first keep a flag variable, which becomes 0 when number is divisible by any number <n. Also, run the for loop from 2 to n-1.

Refer to the code below:

import java.util.*;
import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int i;
		int flag = 1;
		for (i = 2; i < n; i++) {
			if (n % i == 0) {
				// System.out.println("Not Prime");
				flag = 0;
			}
			// else
			// System.out.println("Prime");
		}
		if(flag==0)
			System.out.println("Not Prime");
		else
			System.out.println("Prime");

	}
}

runguard: warning: timelimit exceeded (wall time): aborting command
runguard: warning: command terminated with signal 15
sir still showing same kind of error

@karthik1989photos,
Can you share the code you submitted?

when i click on submit it is showing the same error

@karthik1989photos,
Please share the code you submitted

import java.util.*;
import java.util.Scanner;

public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i;
int flag = 1;
for (i = 2; i < n; i++) {
if (n % i == 0) {
// System.out.println(“Not Prime”);
flag = 0;
}
// else
// System.out.println(“Prime”);
}
if(flag==0)
System.out.println(“Not Prime”);
else
System.out.println(“Prime”);

}

}

@karthik1989photos,

https://ide.codingblocks.com/s/258592 the code you sent is correct and should submit. Try to refresh the page and submit again.

Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:7)
on submitting code it is showing this error after refreshing the page also

@karthik1989photos,
Okay. Kindly drop a mail to [email protected] regarding the issue. It is a technical issue because the code is correct and I have myself verified it with the test cases.

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.