I have implemented it my way. Hope this helps.
import java.util.Scanner;
class PrimeNumbers
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
System.out.println("Enter the value of m:");
int m = scanner.nextInt();
for (i = n; i <= m; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from m to n are :");
System.out.println(primeNumbers);
}
}
1
solved what is wrong in my program ? i am trying to find the list of prime number from n to m