Navcool
Sep 11, 2013, 09:53 AM
In the main method, read the start and end integers. In a separate function, find the prime numbers that are available in between the start and end integers given in the main. Return the founded prime numbers back to the main (think we have to use arrays) and print it in the main by calling the function.
I have done part of the code like this but I don't know how to find the prime numbers between it.. pls correct the code!
import java.util.*;
public class primeno {
public static void main (String[]args)
{
Scanner in = new Scanner (System.in);
System.out.println("Enter the start and end numbers: ");
int a = in.nextInt();
int b = in.nextInt();
int[] result= search(a, b);
System.out.println(result);
}
public static int [] search (int x, int y) {
int count =0;
int len = y-x;
int [] arr = new int[len];
for (int i=x; i<=y; i++)
{
arr[count] = i;
count++;
for (int j=2; j<i; j++) {
int d = (i)%j;
if (d==0) {
//not a prime number
break;
}
}
}
return arr;
}
}
I have done part of the code like this but I don't know how to find the prime numbers between it.. pls correct the code!
import java.util.*;
public class primeno {
public static void main (String[]args)
{
Scanner in = new Scanner (System.in);
System.out.println("Enter the start and end numbers: ");
int a = in.nextInt();
int b = in.nextInt();
int[] result= search(a, b);
System.out.println(result);
}
public static int [] search (int x, int y) {
int count =0;
int len = y-x;
int [] arr = new int[len];
for (int i=x; i<=y; i++)
{
arr[count] = i;
count++;
for (int j=2; j<i; j++) {
int d = (i)%j;
if (d==0) {
//not a prime number
break;
}
}
}
return arr;
}
}