+ 9
Can anyone help me to implement this code using Java programming?
4 Answers
+ 25
all factors of a number will be <=num/2
//so u can run loop till int (num/2) only
//do u want someone to re-write this in java ?
import java.util.Scanner;
public class GauravProgram{
public static void main(String[] args){
Scanner a=new Scanner(System.in);
int x=a.nextInt(); //done @Saleh âș
boolean b=(x>1)?true:false;
for (int i=2;i<=x;i++)
{
int c=x%i;
if (c==0)
b=false ;
}
if(b)
System.out.println(x+" is prime number");
else
System.out.println(x+" is not prime number");
}
}
+ 5
It will be more helpful @Gaurav
+ 5
Thanks
+ 3
import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}