everyone, I tried this program but not get the expected answer, Please help me out.
Question is:
Print all prime numbers between two given numbers. For example: Function is Prime(beg,end)
, for Prime(4,7)
it will return {5,7}, for Prime(7,16)
it will return {7,11,13}.
My code :
package com.robin.practise;
import java.util.Scanner;
public class PrimebetweenRange
{
private static String Prime(int beg, int end)
{
String res= " ";
for(int i= beg; i<= end; i++)
{
for(int j=2; j<= end; j++)
{
if(i%j!= 0)
{
//System.out.println(i+ " ");
res= i+ " ";
}
}
}
return res;
}
public static void main(String[] args)
{
Scanner scn= new Scanner(System.in);
System.out.println("Enter any two numbers: ");
int n1= scn.nextInt();
int n2= scn.nextInt();
scn.close();
System.out.println(Prime(n1, n2));
}
}