AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
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 352
programming x 168
Posted On : 2016-10-04 19:59:04.0
profile Akash Prasad - anyforum.in Akash Prasad
2550
up-rate
5
down-rate

Answers


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 : 2 Yes  0 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
3
down-rate
Comments
I am satisfied but my test cases are not passed yet but answer is correct
profile Akash Prasad - anyforum.in Akash Prasad
25  5  0
Posted On :2016-10-05 11:46:29.0
Leave a Comment
and when i enter input 4 10 then the output will be ,5,7 why at first place comma is coming ??
profile Akash Prasad - anyforum.in Akash Prasad
25  5  0
Posted On :2016-10-05 11:49:33.0
Leave a Comment

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<Integer> 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 : 2 Yes  1 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
5
down-rate
Comments
Not satisfied test cases are not passed
profile Akash Prasad - anyforum.in Akash Prasad
25  5  0
Posted On :2016-10-08 11:43:46.0
Leave a Comment
May i know which test case that not satisfied .can you provide that test case.
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0  96  0
Posted On :2016-10-08 16:15:21.0
Leave a Comment
the last commend that u used of bracket is not in the output so changed it thats why the test cases are not passed
profile Akash Prasad - anyforum.in Akash Prasad
25  5  0
Posted On :2016-10-10 14:00:59.0
Leave a Comment

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
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
2
down-rate
Comments
This code is only satisfied 1 to 10 range.but not satisfied other cases.
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0  96  0
Posted On :2016-10-10 19:42:47.0
Leave a Comment

//Program in java for prime numbers between M and N separated with ","
import java.util.Scanner;
class congni2{
public static void main(String []args){
String s="";
int l,m=0;
Scanner sc=new Scanner(System.in);
int M=sc.nextInt();
int N=sc.nextInt();
if(M<0||M==0||N<0||N==0)
{
System.out.println("Invalid Input");
}else{
for(int i=M;i<N;i++){
l=0;
for(int j=i;j>=1;j--){
if(i%j==0){
l=l+1;
}
}
if(l==2){
m=m+1;
if(m==1){
s=s+i;
}else{
s=s+","+i;
}
}
}
System.out.println(s);
}
}
}

Posted On : 2018-02-25 04:53:37
Satisfied : 1 Yes  0 No
profile Rohit Kumar - anyforum.in Rohit Kumar
040
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in