Reputation: 25
i'm trying to write a program in java to print a list of arrays. I know there's already Array.toString(arr) method, but i don't want the "[..]" on the list. I wrote some simple code to do so.
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int[] v = new int[10000];
for(int i = 0; i <= t; i++){
int m = in.nextInt();
int n = in.nextInt();
int list = 0;
for(int min = m; min < n; min++){
if(isPrime(min) == true){
v[list] = min;
list++;
}
}
System.out.printf(("%d \n"), list + ("\n \n"));
}
}
public static boolean isPrime(int num){
int sqrt = (int) Math.sqrt(num) + 1;
for(int i = 2; i < sqrt; i++){
if(num % i == 0)
return false;
}
return true;
}
Say that this is the Input
1
1 10
and the Output is
2
3
5
7
in this exact order and formatting. What am i doing wrong?
Upvotes: 1
Views: 118
Reputation: 1575
this will solve your error
System.out.printf(("%d \n"), list,( "\n \n"));
But if you want to print prime numbers then you need to print num
instead of list
for(int min = m; min < n; min++){
if(isPrime(min) == true){
v[list] = min;
list++;
System.out.printf(("%d \n"), min,( "\n \n"));
}
}
}
Upvotes: 2