//PRIME Number within a range
public void isPrime_1(int s1, int s2) {
int flag = 0, i, j;
//PRIME Number using math.sqrt
//PRIME number using while loop
public class PrimeNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrimeNumber p = new PrimeNumber();
System.out.println("====method1========");
p.isPrime_1(10,100);
System.out.println("===================");
public void isPrime_1(int s1, int s2) {
int flag = 0, i, j;
System.out.println ("The prime numbers in between the entered limits are :");
for(i = s1; i <= s2; i++)
{
for( j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = 0;
break;
}
else
{
flag = 1;
}
}
if(flag == 1)
{
System.out.println(i);
}
}
}
public class PrimeNum {
public static void main(String[] args)
{
Scanner pnc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = pnc.nextInt();
if (IsPrimeNumber(n))
{
System.out.println(n + " is a prime number");
} else
{
System.out.println(n + " is not a prime number");
}
}
public static boolean IsPrimeNumber(int n) {
if (n <= 1)
{
return false;
}
for (int i = 2; i < Math.sqrt(n); i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
}
public static void main(String[] args)
{
Scanner pnc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = pnc.nextInt();
if (IsPrimeNumber(n))
{
System.out.println(n + " is a prime number");
} else
{
System.out.println(n + " is not a prime number");
}
}
public static boolean IsPrimeNumber(int n) {
if (n <= 1)
{
return false;
}
for (int i = 2; i < Math.sqrt(n); i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
}
public class PrimeNumberWhileLoop {
public static void main(String[] args) {
Scanner pn = new Scanner(System.in);
System.out.print("Enter a number : ");
int number = pn.nextInt();
int i = 2;
boolean flag = false;
while(i <= number/2)
{
// condition for not a prime number
if(number % i == 0)
{
flag = true;
break;
}
++i;
}
if (!flag)
System.out.println(number + " :is a prime number.");
else
System.out.println(number + " :is not a prime number.");
}
}
In method 1 where is start range and end range initialized?
ReplyDeletestartrange and endrange will be the input as per requirement. If qus is to find prime number between range 10 to 100 then startrange will be 10 and endrange will be 100
DeleteI dont think it will work.
DeleteWe can discuss on other IM
I have updated with two input parameters for start range as s1 and end range as s2
DeleteNot able to undertsand the use of loops in method 2 and 3
ReplyDeleteIn 3rd method we are using num/2 as the max limit for while loop because, the smallest multiple that will not make it a prime is 2. If you have checked all the numbers from 0 to num/2, If multiple by 2 is bigger than n, then a multiple of 3 or 4 etc will also be bigger than num.
DeleteSo the largest factor for any number num must be <= num/2
like 10, if you can not divide it from 2-5 it is prime, 6-9 are not going to divide ii.
num = the input number which we want to check
In 2nd method we have used math.sqrt, If a number is not a prime, it can be factored into two factors f1 and f2, If f1 and f2 are > the sqrt of the number, f1*f2 would be > the number. So at least one of those factors must be <= to the sqrt of the number. To see if a number is actually prime, we only need to test factors that are <= to the sqrt.