im suppose to Ask from The user enters two integers, and inputs them through the console The first is n, and the second is m. Print all the prime numbers up to n that are in between The difference is exactly m.
this is my code:
import java.util.Scanner;
public class EX1_A {
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
System.out.println("please enter the the limit if the prime number you want");
int n = in.nextInt();
System.out.println("please enter the gab");
int m = in.nextInt();
int arr [] = new int [n];
int c= 0;// c is arr.length
for(int i=2;i<=n;i )
{
int j = 0;
j ;
if(Prime(i))
{
arr[j]=i;
c ;
}
}
int k = c;
while(k>0)
{
for( int j = 0; j<k;j )
{
if(arr[k]-arr[j]==m)
{
System.out.println("the prime numbers with the gap " m " is: {" arr[k] "," arr[j] "}");
}
}
k--;
}
}
public static boolean Prime(int number)
{
for(int i=2; i<number; i )
{
if(number%i == 0)
return false;
}
return true;
}
}
CodePudding user response:
Change internal if where you find and save prime numbers.
int c = 0;// c is arr.length
for (int i = 2; i <= n; i ) {
if (Prime(i)) {
arr[c] = i;
c ;
}
}
CodePudding user response:
This version works:
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("please enter the the limit if the prime number you want");
int n = in.nextInt();
System.out.println("please enter the gab");
int m = in.nextInt();
int arr [] = new int [n];
int c= 0;// c is arr.length
for(int i=2;i<=n;i )
{
if(Prime(i))
{
arr[c]=i;
c ;
}
}
int k = c;
while(k>0)
{
for( int j = 0; j<k;j )
{
if(arr[k]-arr[j]==m)
{
System.out.println("the prime numbers with the gap " m " is: {" arr[k] "," arr[j] "}");
}
}
k--;
}
}
public static boolean Prime(int number)
{
for(int i=2; i<number; i )
{
if(number%i == 0)
return false;
}
return true;
}