AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
3. Our Experts are looking for yours ?.

# corejava-programming: Print the series of prime numbers in java

 Write a java program to print a comma (,) separated series of prime numbers between M and N including. Check if the inputs are correct or not if not then print Invalid input. Sample input:- 1 10 Sample output:- 2,3,5,7 Input Explanation:- First number 1 is the starting point of the series and last number 10 is the tail of the series. corejava x 353programming x 169 Posted On : 2016-10-04 19:59:04.0 Akash Prasad 2550 5

 Check this code and let me know . import java.util.Scanner; class PrimeNumbers { public static void main (String[] args) { int i =0; int num =0; String primeNumbers = ""; Scanner s=new Scanner(System.in); int M=s.nextInt(); int N=s.nextInt(); if(M<0||M==0||N<0||N==0) { System.out.println("Invalid Input"); System.exit(0); } else { for (i = M; i <= N; i++) { int counter=0; for(num =i; num>=1; num--) { if(i%num==0) { counter = counter + 1; } } if (counter ==2) { if(i==2) { primeNumbers +=i; } else { primeNumbers +=","+i; } } } } System.out.print(primeNumbers); System.out.println(); } } Posted On : 2016-10-05 01:44:31 Satisfied : 4 Yes  2 No 3
I am satisfied but my test cases are not passed yet but answer is correct
25  5  0
Posted On :2016-10-05 11:46:29.0
and when i enter input 4 10 then the output will be ,5,7 why at first place comma is coming ??
25  5  0
Posted On :2016-10-05 11:49:33.0

 I have modified the code ,check this below code and let me know if your test cases are satisfied or not. import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PrimeNumbers { public static void main(String[] args) { Scanner s = new Scanner(System.in); int M=s.nextInt(); int N = s.nextInt(); s.close(); List primes = new ArrayList<>(); if(M<0||M==0||N<0||N==0) { System.out.println("Invalid Input"); System.exit(0); } else { for (int i = M; i < N; i++) { boolean isPrimeNumber = true; for (int j = 2; j < i; j++) { if (i % j == 0) { isPrimeNumber = false; break; } } if (isPrimeNumber) { if(i==1) { primes.remove(new Integer(1)); } else primes.add(i); } } String s1 = primes.toString().replace("[", "").replace("]", ""); System.out.println(s1); } } } Posted On : 2016-10-05 14:04:36 Satisfied : 3 Yes  1 No 5
Not satisfied test cases are not passed
25  5  0
Posted On :2016-10-08 11:43:46.0
May i know which test case that not satisfied .can you provide that test case.
Posted On :2016-10-08 16:15:21.0
the last commend that u used of bracket is not in the output so changed it thats why the test cases are not passed
25  5  0
Posted On :2016-10-10 14:00:59.0

 Small improvisation in first code . Check this code and let me know your test cases are satisfied or not. ============================================================ import java.util.Scanner; import java.text.DecimalFormat; class PrimeNumbers { public static void main (String[] args) { int i =0; int num =0; String primeNumbers = ""; Scanner s=new Scanner(System.in); int M=s.nextInt(); int N=s.nextInt(); DecimalFormat decimalFormat = new DecimalFormat("#,#"); if(M<0||M==0||N<0||N==0) { System.out.println("Invalid Input"); System.exit(0); } else { for (i = M; i <= N; i++) { int counter=0; for(num =i; num>=1; num--) { if(i%num==0) { counter = counter + 1; } } if (counter ==2) { primeNumbers +=i; } } } int result = Integer.parseInt(primeNumbers); String number = decimalFormat.format(result); System.out.print(number); System.out.println(); } } Posted On : 2016-10-10 19:38:51 Satisfied : 0 Yes  1 No 2