In: Computer Science
Prime Number
Determines if a number is prime or not
I also need the algorithm and pseudocode in java.
ANSWER: Here I am giving you the algorithm and code please like it.
ALGORITHM:
START:
Declare variables n,i,flag
Intialize variables flag=1,i=2
read number n from user
if n<=1
Display "n is not a prime number"
Repeat the steps using for loop until i<[(n/2)+1]
if remainder of n divide i equals to 0,
Set flag=0
if flag==0,
Display "n is not prime number"
else
Display "n is prime number"
STOP
CODE:
import java.util.*;
public class Main {
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
int n,i,flag;
flag=1;
i=2;
System.out.println("Enter a number : ");
n=sc.nextInt();
if(n<=1)
System.out.println(n+" is not a
prime number.");
while(i<((n/2)+1))
{
if(n%i==0)
flag=0;
i++;
}
if(flag==0)
System.out.println(n+" is not a
prime number.");
else
System.out.println(n+" is a prime
number.");
}
}
OUTPUT: